使用 jQuery 选择器和 setSelectionRange 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17158802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jQuery selector and setSelectionRange is not a function
提问by srg2k5
I have assembled a basic jfiddle below. For some reason my selector works to retrieve the textarea box to set the value, but the selector doesnt work to use the setSelectionRange function. On the console you'll find an error for .setSelectionRange is not a function.
我在下面组装了一个基本的 jfiddle。出于某种原因,我的选择器可以检索 textarea 框以设置值,但选择器无法使用 setSelectionRange 函数。在控制台上,您会发现 .setSelectionRange is not a function 的错误。
code(please refer to jfiddle):
selector.setSelectionRange(carat,carat);
代码(请参考jfiddle):
selector.setSelectionRange(carat,carat);
回答by PSL
回答by Prusdrum
For me this is a good solution
对我来说这是一个很好的解决方案
selector[0].setSelectionRange(start ,end);
But I would like to add one more thing. I noticed that setSelectionRange
is something that becomes available asynchronously after the element gets focus.
但我想补充一点。我注意到这 setSelectionRange
是在元素获得焦点后异步可用的东西。
var element = selector[0];
element.addEventListener('focus', function() {
element.setSelectionRange(start, end);
});
element.focus();
Also you can use alternatively:
您也可以选择使用:
element.selectionStart = start;
element.selectionEnd = end;
回答by KingRider
HTML:
HTML:
<input type="search" value="Potato Pancakes" id="search">
JQUERY:
查询:
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;
});
};
$("#search").putCursorAtEnd();
Check:
查看:
http://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
http://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
回答by Outside Edge
You could try this which works for me. I use it to build an address from the separate address fields and then do the copy for pasting.
你可以试试这个对我有用。我用它从单独的地址字段中构建一个地址,然后复制粘贴。
The HTML
HTML
<div id="d_clip_container" style="position:relative">
(<a href="#" id="d_clip_button">copy to clipboard</a>)
</div>;
<textarea id="clip" rows="0" cols="0" style="border:none;height:0;width:0;"></textarea>
The jQuery
jQuery
$(document).ready(function() {
$('#d_clip_button').click(function() {
//get all the values of needed elements
var fName = $("#firstName").val();
var lName = $("#lastName").val();
var address = $("#Address").val();
var city = $("#City").val();
var state = $("#State").val();
var zip = $("#Zip").val();
//concatenate and set "clip" field with needed content
$('#clip').val(fName + " " + lName + "\n" + address + "\n" + city + ", " + state + " " + zip);
//Do it
if(copyToClipboard('#clip')) {
alert('text copied');
} else {
alert('copy failed');
}
});
});
function copyToClipboard(elem) {
// set focus to hidden element and select the content
$(elem).focus();
// select all the text therein
$(elem).select();
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
succeed = false;
}
// clear temporary content
$(target).val('');
return succeed;
}