将某些内容转换为 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
Convert something to Jquery object
提问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 item
is a DOM element. In fact, you'll find yourself doing this a lot in callback functions, since usually this
refers 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');
应该管用
回答by jwl
$(item).attr("whatever")
Jquery takes many different types of arguments, including straight up HTML elements, read the doco
Jquery 接受许多不同类型的参数,包括直接的 HTML 元素,阅读 doco