请参阅以下代码:
<div id="song" class="s1">
<div class = "l">foo</div>
<div class="r">
<div>foo</div><div>foo</div>
<div class="link"><a href="some url">Link</a></div>
</div></div>
<div id="song" class="s2">
<div class = "l">foo</div>
<div class="r">
<div>foo</div><div>foo</div>
<div class="link"><a href="some url">Link</a></div>
</div></div>
<div id="song" class="s3">
<div class = "l">foo</div>
<div class="r">
<div>foo</div><div>foo</div>
<div class="link"><a href="some url">Link</a></div>
</div></div>
还有更多这样的东西。我必须使用JavaScript从第一个<div>
获取其ID为歌曲的链接,因为我已使用php将此数据提取到<object>
标记中。
谢谢:)
答案 0 :(得分:0)
您应该使用JQuery(位于文件底部):
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
var links = $.map($('div#song .link a'), function(a) { return $(a).attr('href'); });
console.log(links);
</script>
输出:
["some url", "some url", "some url"]
答案 1 :(得分:0)
仅注意第一组,
<div id="song1" class="s1">
<div class = "l">foo</div>
<div class="r">
<div>foo</div>
<div>foo</div>
<div class="link"><a href="some url">Link</a></div>
</div>
您可以通过
获取某个网址的链接var link = $(".s1").find("a");
console.log(link.attr('href') );