将某些内容转换为 Jquery 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1049048/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:30:22  来源:igfitidea点击:

Convert something to Jquery object

jquery

提问by RubbleFord

$('#tags option').each(function(index, item) {
    // var i = this;
    //if (jQuery.inArray(i.value, idArray)) {
    // i.attr('disabled', 'true');
    // }
    item.attr('disabled', 'true');
});

How to I convert the item parameter into Jquery object so I can use all the nicety's like .attr?

如何将 item 参数转换为 Jquery 对象,以便我可以使用 .attr 之类的所有细节?

Thanks

谢谢

回答by Peter

You can just wrap it like this:

你可以像这样包装它:

var jQueryItem = $(item);

where itemis a DOM element. In fact, you'll find yourself doing this a lot in callback functions, since usually thisrefers to a DOM element and you'll usually want to operate on that using jQuery API calls.

哪里item是 DOM 元素。事实上,您会发现自己在回调函数中经常这样做,因为通常this指的是 DOM 元素,并且您通常希望使用 jQuery API 调用对其进行操作。

回答by jimyi

$(item).attr('disabled', 'true');should work

$(item).attr('disabled', 'true');应该管用

See: http://docs.jquery.com/Core/jQuery#elements

请参阅:http: //docs.jquery.com/Core/jQuery#elements

回答by jwl

$(item).attr("whatever")

Jquery takes many different types of arguments, including straight up HTML elements, read the doco

Jquery 接受许多不同类型的参数,包括直接的 HTML 元素,阅读 doco