JavaScript DHTML/jQuery/get
Содержание
Get first element in query set
<source lang="html4strict">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ var mappedItems = $("li").map(function (index) {var data = $("
- asdf
- asdf
- asdf
</body>
</html>
</source>
Get tag first parent
<source lang="html4strict">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("*", document.body).each(function () { var parentTag = $(this).parent().get(0).tagName; alert(parentTag); }); }); </script> <style> .y { background:yellow; } </style> </head> <body> <body>
<button disabled="disabled">First</button>
</body>
</html>
</source>
Gives the tag name of the element clicked on.
<source lang="html4strict">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("*", document.body).click(function (e) { e.stopPropagation(); var domEl = $(this).get(0); $("span:first").text("Clicked on - " + domEl.tagName); }); }); </script> </head> <body> <body>
A
B
</body>
</html>
</source>
Uses the built-in reverse-method to reverse array
<source lang="html4strict">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ alert( $("p").get().reverse() ); }); </script> </head> <body> <body>
A
B
</body>
</html>
</source>