如何检测DOM元素的class属性何时发生变化?

时间:2013-04-21 15:57:48

标签: javascript html selection

这需要一些解释。我正在使用jquery播放列表的东西,使用JWplayer,基本上它的作用是读取播放列表.xml文件,并生成如下滚动菜单:

<div class="jw_playlist_playlist">
   <div class="jw_playlist_item even">
      <div class="jw_playlist_title">Title</div>
      <div class="jw_playlist_description"></div>
   </div>
   <div class="jw_playlist_item odd">
      <div class="jw_playlist_title">Title2</div>
      <div class="jw_playlist_description"></div>
   </div>
</div>

当选择某个项目时,它会更改为<div class="jw_playlist_item even playing"><div class="jw_playlist_item odd">。有没有办法用javascript检测这个变化,并提取标题?

1 个答案:

答案 0 :(得分:1)

我猜你可以用JavaScript创建一个计时器,如果元素存在,每隔X秒检查一次。

<script> 

var title = ""; 

$(function() { // wait till page is loaded

    // sets a timer to repeat the function every 1 second
    setInterval(function() { 
        // if .play class div exists, take the title of the song
        if ($(".play").length > 0) { 
            title = $(".play jw_playlist_title").html();
            updateData();
        } else { 
        // otherwise set the title to be empty
            title = ""; 
        } 
     }, 1000); 

});

function updateData() {
    $("#demo").html("Current Song: "+title);
}


 </script> 

 <p id="demo">My First Paragraph</p>