JavaScript DOM对象到jQuery对象 [英] JavaScript DOM object to jQuery object
问题描述
如何将JavaScript DOM对象转换为jQuery对象?
How can I convert a JavaScript DOM object to a jQuery object?
<tr onclick="changeStatus(this)">
function changeStatus(myObject) {
XXX.removeClass();
}
我应该写什么代替XXX?
我知道我可以使用id和id选择器做一个解决方法,但它不是那么优雅。
有没有办法将js DOM对象转换为jQuery对象,或者在jQuery中使用这个
关键字?
What should I write in place of XXX?
I know I can make a workaround using an id and an id selector, but it is not so elegant.
Is there any way to convert a js DOM object to a jQuery object or using the this
keyword in jQuery?
推荐答案
var $this = $(myObject);
$ this
是一个jQuery对象。您可以从DOM元素创建jQuery对象。
$this
is a jQuery object. You can create jQuery objects from DOM elements.
<tr onclick="changeStatus(this)">
function changeStatus(myObject) {
$(myObject).removeClass();
}
我想推荐使用jQuery做事情绑定: p>
I would like to recommend doing your event binding with jQuery as well:
<tr class="change-status">
$('.change-status').on('click', function () {
$(this).removeClass( ... );
});
这很好,因为现在所有的JS代码都在一个地方,可以更新(在我看来)更容易。请注意,如果要绑定到所有< tr> $ c,则不需要添加到
< tr>
元素的类$ c>元素。
This is nice because now all the JS code is in one place and can be updated (in my opinion) more easily. Note that the class I added to the <tr>
element is not necessary if you want to bind to all <tr>
elements in the DOM.
这篇关于JavaScript DOM对象到jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!