1

我想继续检查新的 li 元素,以便每当有这些新元素时,我都会收到新的警报?

JS:

$(function () {

    if ($('#div').find('<li></li>')) {
        alert('you just got new <li> elements');
    }

    // it is just an examplory function which                       
    // will keep looking for only  new <li> elements created by ol list

})(); 

Js 小提琴在这里http://jsfiddle.net/younis764/rWcKu/3/

请帮忙。

4

1 回答 1

3

像这样的功能应该可以帮助您

document.addEventListener("DOMNodeInserted", function(event){
  var element = event.target;
  if (element.tagName == 'li') {
     alert("li added");    
  }
});

检查突变事件

小提琴演示

HTML

<div id="container" style="background-color:#e0d0a0; width:300px; height:100px;"></div>   
<button onclick="AddLiToContainer ();">Add a li node to the container!</button>

JAVASCRIPT

 function AddLiToContainer() {
     var newLi = document.createElement("li");
     var textNode = document.createTextNode("Hello World");
     newLi.appendChild(textNode);
     if (newLi.addEventListener) {
         newLi.addEventListener('DOMNodeInserted', OnNodeInserted, false);
     }

     var container = document.getElementById("container");
     container.appendChild(newLi);
 }

 function OnNodeInserted(event) {
     var Li = event.target;
     alert("The text node '" + Li.innerHTML + "' has been added to an element.");
 }
于 2013-11-12T07:20:10.973 回答