Possible Duplicate:
jQuery $(this) vs this
This code in this video Tutorial
in a Very useful blog jquery for designers
$('.navigation').each(function () {
var $links = $('a',this);
$links.click(function () {
var $link = $(this),
link = this;
if ($link.is('.selected')) {
return;
}
$links.removeClass('selected');
$link.addClass('selected')
});
});
What is the difference between $(this) and this?
Please explain the simple difference in coding.
Within the handler being pased into
click,thiswill refer to the DOM element that the click handler was attached to. Calling$()on it ($(this)) wraps that up in a jQuery instance, making various jQuery functions available to you.In your quoted code, the
link = thisline is unnecessary as nothing ever useslink. The$link = $(this)line, though, creates a jQuery wrapper around the link so you can use functions likeisandaddClass.Off-topic
if ($link.is('.selected'))toif ($link.hasClass('selected'))so that jQuery doesn’t have to parse the CSS selector.removeClassis missing.