[英]How to make value of <input type=“text”> selected with jQuery?
假設輸入的jQuery對象是$ input。
如何選擇其值(突出顯示)?
假設元素的id是anInput
,您應該能夠執行以下操作:
var $input = $("#anInput");
$input[0].select();
$ input表示包裝的set或jQuery對象,就像你所說的那樣。 該集合的零值索引是您的選擇器匹配的第一個DOM元素(在這種情況下只有一個)。 有了這個元素,select函數應該選擇它的內容。
這是你需要的嗎?
嘗試這個:
$('input, select, textarea').each(function() {
$(this).focus(function() {
if (this.select) {
this.select();
}
});
});
聚焦您使用的元素
$(input).focus();
要突出顯示它,您需要創建一個額外的高亮CSS類,在onfocus和blur事件中執行您想要的突出顯示(或直接修改元素樣式)。
$(input).focus(function() { $(this).addClass("highlight"); });
$(input).blur(function() { $(this).removeClass("highligh"); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.