您可以使用Input.setSelectionRangeRange API的一部分来执行此操作,以与文本选择和文本光标进行交互:
var searchInput = $(’#Search’);// Multiply by 2 to ensure the cursor always ends up at the end;// Opera sometimes sees a carriage return as 2 characters.var strLength = searchInput.val().length * 2;searchInput.focus();searchInput[0].setSelectionRange(strLength, strLength);解决方法
我有以下功能,将选择器添加到搜索输入中作为高级选项,就像堆栈溢出高级搜索一样。
当您单击要搜索的内容时,它会添加一个前缀。请参阅下面的Jquery:
<script>$(document).ready(function () {$('table#advanced_search_options tr').click(function () { var SearchSelection = $(this).children('td:last-of-type').html(); var SearchInput = $(’#Search’); SearchInput.val(SearchInput.val() + SearchSelection); return false; alert(SearchSelection);});});</script>
如何处理以上内容,使焦点也集中在#search输入上,将胡萝卜(闪烁的文本插入光标)放在插入的文本/值的末尾?例如。
HC:<-添加到我的搜索输入中的值,我想在此处设置光标: