我是javascript和英语不好的新手。
1)我有这样的HTML:
<ul id="search-list" class="playlist1">
<li class="song songW" data-song-id="70">
<div id="songNum" class="songNum" songurl="music/turn-it-up.mp3"
songtitle="Turn It Up">Turn It Up
</div>
</li>
<li class="song songB" data-song-id="83">
<div id="songNum" class="songNum" songurl="music/black-keys.mp3"
songtitle="Black Keys">Black Keys </div>
</li>
</ul>
2)我使用这个Javascript从点击的DIV获取属性有类或id =“songNum”:
var el = $("ul#search-list");
$(el).bind(".songNum").click(function(eve) {
eve.preventDefault();
songUrl = $(this).attr("songurl");
alert(songUrl);});
///// It's return "Undefined"
3)当我改变时
var el = $("ul#search-list");
$(el).bind(".songNum").click(function(eve) {
eve.preventDefault();
songUrl = $(".songNum").attr("songurl");
alert(songUrl); });
///// It's just return a atrribute of the first DIV =>"music/turn-it-up.mp3", even i was click on the second DIV
请帮助我,告诉我我的错误! 非常感谢!!!
答案 0 :(得分:0)
var el = $("ul#search-list li");
$(el).bind(".song").click(function(eve) {
eve.preventDefault();
songUrl = $(this).find('div').attr("songurl");
alert(songUrl);
});
应该有效。使用$(this)
来获取被点击的第一个div的上下文,而不是在里面找到子div,然后你可以访问它的属性。
希望有所帮助