<script type="text/javascript">
$(document).ready(function(){
$("div").each(function(){
alert($(this).html() + "----" + this.tagName + "--" + $(this)[0].id);
alert((this == $(this)[0]));
$(this).attr("title", $(this).html());
});
});
</script>
</head>
<body>
<div id="node1">sss1</div>
<div id="node2">sss2</div>
</body>
#这里的this其实是一个Html元素(div),获取html属性值均可用this.属性名来取值
#这里的$(this)是一个JQuery对象,可以调用JQuery的方法
# this == $(this)[0] 返回为true
#JQuery拥有attr()方法可以get/set DOM对象的属性
jQuery操作DOM实例
本文通过一个具体的示例展示了如何使用jQuery来遍历和操作页面上的div元素,包括获取元素的内容、标签名及ID,并设置元素的title属性。此外,还介绍了this与$(this)的区别及其在jQuery中的应用。
248

被折叠的 条评论
为什么被折叠?



