<div>Zero-th <span></span></div> <div>First <span></span></div> <div>Second <span></span></div> <script> $("div").attr("id", function (arr) { return "div-id" + arr; }).each(function () { $("span", this).html("(ID = '<b>" + this.id + "</b>')"); }); /*//下面这种写法和上main写法一样 $("div").attr("id", function (arr) { return "div-id" + arr; }).each(function () { $(this).find('span').html("(ID = '<b>" + this.id + "</b>')"); }); */ </script>也即是:$('span',this)相当于$(this).find($('span'))
$('span',this)
最新推荐文章于 2024-03-10 16:01:55 发布
本文介绍使用jQuery库为页面元素动态设置属性并遍历元素的方法,通过具体实例展示了如何利用jQuery简化DOM操作,包括为多个div元素设置id属性,并在每个div内的span标签中显示对应的id。
973

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



