Hi there, I wonder if anybody can help me with this. I a a bit confused as to what $(this) and what e.target.nodeName represent in jquery. I had a look at this http://api.jquery.com/event.target/ but I am still not entirely sure, I hope somebody can clarify this. I run a little test of course, trying to:
1)clarify what $(this) and e.target.nodeName (and when this distinction is useful)
2)whether the 2 are the same - and from what I read in the API they are not necessarily the same, but in my test it looks like they are.
here's the code:
HTML:
<!DOCTYPE HTML>
<html lang="en" dir="ltr">
<head>
<title>tst</title>
<link rel = "stylesheet" type = "text/css" href = "style.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class = "me">
<table border="1">
<tbody>
<tr>
<td>
<span>hey het <a href="#">this is a link <img src="ajax-loader.gif" alt=""></a></span>
</td>
<td>ghtd</td>
</tr>
</tbody>
</table>
<!-- NAVIGATION -->
<div id="navBox">
<ul>
<li class="mainList"><a href="#">Item 1</a>
<ul class="subList">
<li><a href="#">sub item1.1</a></li>
<li><a href="#">sub item1.2</a></li>
<li><a href="#">sub item1.3</a></li>
</ul>
</li>
<li class="mainList"><a href="#">Item 2</a>
<ul class="subList">
<li><a href="#">sub item2.1</a></li>
<li><a href="#">sub item2.2</a></li>
<li><a href="#">sub item2.3</a></li>
</ul>
</li>
<li class="mainList"><a href="#">Item 3</a>
<ul class="subList">
<li><a href="#">sub item1.1</a></li>
<li><a href="#">sub item1.2</a></li>
<li><a href="#">sub item1.3</a></li>
</ul>
</li>
</ul>
</div>
<!-- END OF NAVIGATION -->
</div>
</body>
</html>
CSS (never know it might be useful)
.me{width:900px;
height:800px;
/*background:url(ajax-loader.gif) 100px 40px no-repeat;*/
border:1px solid red;}
table a img{
vertical-align:-3px;
}
#navBox{
border:1px solid yellow;
width:400px;
}
/*
#navBox ul > li > ul{
display:none;
}
*/
.subList {display:none}
.visible{
display:block;
}
and js
$(function(){
$("li.mainList > a").click(function(e){
/*testin e.target.nodeName*/
var MyTarget = e.target.nodeName;
console.log("'This' is " + $(this).text() + " and 'e.target.nodeName' is " + e.target.nodeName);
//if($(this) == MyTarget){
console.log("my target is " + MyTarget );
//}
//else console.log("this is not myTarget");
/*testin e.target.nodeName*/
$("ul.subList").removeClass("visible");
$(this).parent().find("ul").addClass("visible");
});
});
When I print on the console $(this).text() returns the text of the link I clicked on and e.target.nodeName the element I clicked on "A" (incidentally why does it return it in upper case?!)