使用jQuery按类访问元素

时间:2022-04-15 14:28:08

I have a table with an ID of InstrumentListGrid. When a row is selected, it sets the class to ui-iggrid-activerow. I want to add a jQuery event on that row for when someone clicks it.

我有一个ID为InstrumentListGrid的表。选择行时,它会将类设置为ui-iggrid-activerow。我希望在有人点击它时在该行上添加一个jQuery事件。

So far I have

到目前为止我有

$("#InstrumentListGrid tr.ui-iggrid-activerow").click(function (event) {
    alert("YAY!");
});

but that isn't firing. How do I bind to an element by class?

但那不是解雇。如何按类绑定元素?

2 个解决方案

#1


2  

since the class is presumably added dynamically you should use .delegate()

因为这个类可能是动态添加的,你应该使用.delegate()

$('#InstrumentListGrid').delegate('.ui-iggrid-activerow', 'click', function (e) {
    // do stuff.
});

#2


0  

It appears that ui-iggrid-activerow is dynamically added. Use the live() function:

看来ui-iggrid-activerow是动态添加的。使用live()函数:

$('#InstrumentListGrid tr.ui-iggrid-activerow').live('click', function() {
    alert('YAY!');
});

#1


2  

since the class is presumably added dynamically you should use .delegate()

因为这个类可能是动态添加的,你应该使用.delegate()

$('#InstrumentListGrid').delegate('.ui-iggrid-activerow', 'click', function (e) {
    // do stuff.
});

#2


0  

It appears that ui-iggrid-activerow is dynamically added. Use the live() function:

看来ui-iggrid-activerow是动态添加的。使用live()函数:

$('#InstrumentListGrid tr.ui-iggrid-activerow').live('click', function() {
    alert('YAY!');
});