简体   繁体   English

如何创造价值 <input type=“text”> 用jQuery选中?

[英]How to make value of <input type=“text”> selected with jQuery?

Suppose the jQuery object of the input is $input. 假设输入的jQuery对象是$ input。

How to make its value selected(make it highlighted)? 如何选择其值(突出显示)?

Let's say the id of the element is anInput , you should be able to do the following: 假设元素的id是anInput ,您应该能够执行以下操作:

var $input = $("#anInput");
$input[0].select();    

$input represents the wrapped set or jQuery object, as you put it. $ input表示包装的set或jQuery对象,就像你所说的那样。 The zeroeth index of this set is the first DOM element matched by your selector (there is only one in this case). 该集合的零值索引是您的选择器匹配的第一个DOM元素(在这种情况下只有一个)。 With that element in hand, the select function should select its contents. 有了这个元素,select函数应该选择它的内容。

Is this what you need? 这是你需要的吗?

Try this: 尝试这个:

$('input, select, textarea').each(function() {
    $(this).focus(function() {
        if (this.select) {
            this.select();
        }
    });
});

To focus the element you use 聚焦您使用的元素

$(input).focus();

To highlight it you create an extra highligh CSS class that does the Highlighting you want (or you directly modify the element style) in the onfocus and blur events. 要突出显示它,您需要创建一个额外的高亮CSS类,在onfocus和blur事件中执行您想要的突出显示(或直接修改元素样式)。

$(input).focus(function() { $(this).addClass("highlight"); });
$(input).blur(function() { $(this).removeClass("highligh"); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM