admin管理员组文章数量:1387381
$('div.box').html("hello")
puts the word hello inside all my div's of class 'box'. They all have an assigned id.
I'm trying to figure out how to put each div's id instead of 'hello' inside the div. Maybe this is really easy, but I can't figure it out. I'm also new to jQuery. Any ideas? I've tried:
$("div.box").html("id: " + $(this).attr("id"));
$('div.box').html("hello")
puts the word hello inside all my div's of class 'box'. They all have an assigned id.
I'm trying to figure out how to put each div's id instead of 'hello' inside the div. Maybe this is really easy, but I can't figure it out. I'm also new to jQuery. Any ideas? I've tried:
$("div.box").html("id: " + $(this).attr("id"));
Share
Improve this question
edited Dec 9, 2009 at 9:01
cletus
626k169 gold badges919 silver badges945 bronze badges
asked Feb 8, 2009 at 4:09
bbarnhartbbarnhart
6,7201 gold badge44 silver badges60 bronze badges
1
- Well that was weirde... accepted then unaccepted. Fleeting. :) – cletus Commented Feb 8, 2009 at 12:24
2 Answers
Reset to default 8Try this:
$("div.box").each(function() {
$(this).html("Id: " + this.id);
});
Full example:
<html>
<head>
<title>Layout</title>
</head>
<body>
<div id="id1" class="box">Box One</div>
<div id="id2" class="box">Box Two</div>
<div id="id3">Box Three</div>
<div id="id4" class="box">Box Four</div>
<script src="http://www.google./jsapi"></script>
<script>
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function() {
$(function() {
$("div.box").each(function() {
$(this).html("ID: " + this.id);
});
});
});
</script>
</body>
</html>
You'll want to loop through each item.
$("div.box").each(function()
{
$(this).html("id: " + this.id);
});
本文标签: Putting javascript id inside div with jQueryStack Overflow
版权声明:本文标题:Putting javascript id inside div with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744565381a2613014.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论