开发者

toggleClass not working in live

开发者 https://www.devze.com 2023-03-20 21:58 出处:网络
I have a problem below is my code. i have use toggleClass and passed a css class in it. Now if i use without live() method it is working fine. But if i use with live() method it is not working i am st

I have a problem below is my code. i have use toggleClass and passed a css class in it. Now if i use without live() method it is working fine. But if i use with live() method it is not working i am stuck into this problem.

//Without live() method and it is working fine

$('#shownColumnsDiv tbody tr').click(function () {
        globalMove = $(this);
        glob开发者_运维问答alArray.push(globalMove);
        index = $('#shownColumnsDiv tbody tr').index(this);
        alert(index);
        globalMove.toggleClass('highlight');            
    });

//With live() method it is not working fine

$('#shownColumnsDiv tbody tr').live("click", function(){
        globalMove = $(this);
        globalArray.push(globalMove);
        index = $('#shownColumnsDiv tbody tr').index(this);
        alert(index);
        globalMove.toggleClass('highlight');
    });

//this is a css portion

.highlight {
background-color: #0078ae;
color: white;
}


Try:

globalMove.toggleClass('highlight');

globalMove is already a jQuery object, because you stored $(this) in it, why would you want to generate a jQuery object of the jQuery object by writing $(globalMove)?

Looks like it's working fine for me?


use .on instead of live.

it is now no longer recommeded. Now with .live() being deprecated in jQuery version 1.7 and removed in version 1.9, we need to use the .on() method. Here's an equivalent example using the .on() method:

$(document).on('click', 'button', doSomething);

function doSomething() {
    // do something
}
0

精彩评论

暂无评论...
验证码 换一张
取 消