Monday, 15 June 2015

javascript - Rationale behind $(this) in jQuery -



javascript - Rationale behind $(this) in jQuery -

i trying produce little script using jquery , encountered roadblock. script turning each clicked objects of table in specific style in css using class .trhighlight in script below:

$(document).ready(function() { $('tr').bind('click', function() { $(this).addclass('trhighlight'); }); });

i trying turn in normal style when clicking 1 time again on each row of th table (each tr object). tried next script:

$(document).ready(function() { $('tr').bind('click', function() { $(this).addclass('trhighlight'); }); $('tr .trhighlight').bind('click', function() { $(this).removeclass('trhighlight'); }); });

a specific if construction not help much too. have idea? question, have description of properties can adress through this.x this.id, maybe can seek find way using class property of "this".

thanks

you can utilize toggleclass():

$(document).ready(function () { $('tr').bind('click', function () { $(this).toggleclass('trhighlight'); }); });

and might consider alter bind() on(). both work, pros , cons of both reference: jquery .bind() vs. .on(). should utilize .on() if available since .bind() deprecated. sidenote - bind() rewrite function on() - http://james.padolsey.com/jquery/#v=2.0.3&fn=$.fn.bind (actualised info retrieved answer: jquery .bind() vs. .on()

javascript jquery

No comments:

Post a Comment