0

我是 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 中获取属性有 class 或 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

请帮助我,告诉我我的错误!十分感谢 !!!

4

1 回答 1

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,然后您可以访问其属性。

希望有帮助

于 2013-10-13T16:26:27.040 回答