Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将焦点和光标设置为文本输入字段/字符串w的结尾。滑动分页_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 将焦点和光标设置为文本输入字段/字符串w的结尾。滑动分页

Javascript 将焦点和光标设置为文本输入字段/字符串w的结尾。滑动分页,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有下面的函数,它将选择器作为高级选项添加到搜索输入中,就像堆栈溢出高级搜索一样 单击要搜索的内容时,会添加前缀。请参见下面的Jquery: <script> $(document).ready(function () { $("table#advanced_search_options tr").click(function () { var SearchSelection = $(this).children("td:last-of-ty

我有下面的函数,它将选择器作为高级选项添加到搜索输入中,就像堆栈溢出高级搜索一样

单击要搜索的内容时,会添加前缀。请参见下面的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>

$(文档).ready(函数(){
$(“表#高级搜索_选项tr”)。单击(函数(){
var SearchSelection=$(this).children(“td:type的最后一个”).html();
var SearchInput=$(“#搜索”);
SearchInput.val(SearchInput.val()+SearchSelection);
返回false;
警报(搜索选择);
});
});
我如何操作上述操作,将胡萝卜(闪烁的文本插入光标)放在插入的文本/值的末尾,从而将焦点转移到#搜索输入? 例如

HC:您可以使用与文本选择和文本光标交互的部分范围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);

演示:

以防万一:
if(newEl.attr('type')!='number'){}
阻止您尝试在不支持设置插入符号的输入类型上设置插入符号查看此分叉:
http://jsfiddle.net/msLkp1gt/1/
,我希望用户关注第一个,当他们使用tab关注第二个时,光标应该移到末尾。您应该将值长度乘以2,因为Opera有时会将回车符视为2个字符。因此,文本不是每次都选择在结尾。这对我来说不起作用!我将它包装在一个
setTimeout(function(){//code},0)中
正如@DerkJanSpeelman提到的,对我来说,它必须包装在setTimeout 0中