gpt4 book ai didi

javascript - 使用 JS 专注于文本字段并在文本末尾放置标记

转载 作者:行者123 更新时间:2023-11-30 18:37:18 25 4
gpt4 key购买 nike

以下js在queryform中查找textfield q并聚焦:

js:

if($("#queryform").length){
文档.queryform.q.focus();
}

html:

<form id="queryform" name="queryform" action="" method="post"><input type="text" name="q" /></form>

当前函数

在 chrome 中:找到并聚焦文本字段,并突出显示文本

在 FF 和 IE 中:文本框获得焦点,标记放在后面

想要的功能

在所有浏览器中都相同:文本字段获得焦点,文本未突出显示,标记在文本末尾

jquery

我已经在网站上使用了 jquery,所以最好使用简洁的 jquery 解决方案

最佳答案

查看此解决方案:

Use JavaScript to place cursor at end of text in text input element

第一个解决方案不起作用,但他的脚本起作用了,参见 http://jsfiddle.net/WcGLP/

(function($)
{
jQuery.fn.putCursorAtEnd = function()
{
return this.each(function()
{
$(this).focus()

// If this function exists...
if (this.setSelectionRange)
{
// ... then use it
// (Doesn't work in IE)

// Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
var len = $(this).val().length * 2;
this.setSelectionRange(len, len);
}
else
{
// ... otherwise replace the contents with itself
// (Doesn't work in Google Chrome)
$(this).val($(this).val());
}

// Scroll to the bottom, in case we're in a tall textarea
// (Necessary for Firefox and Google Chrome)
this.scrollTop = 999999;
});
};
})(jQuery);

关于javascript - 使用 JS 专注于文本字段并在文本末尾放置标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7882315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com