简体   繁体   中英

Detect div > ul has been changed

I have some test code.

 <div class="someclass" id="someid">
    <div class="form-someclass1 class2 ">
    <div class="button action right addbutton" id="btnId"><i class="add"></i> Add</div>
   </div>
    <ul class="ui-sortable">
           <!-- dynamically li gets added. -->
    </ul>
  </div>

As soon as we click on "btnId" li gets added in ul and div has been changed. Similarly, if we remove li ul gets changed and div has been changed. Is there any way to get information that div > ul has been changed and on which event should we trigger the code in order to know change? I tried for DOMSubTreeNodeChanged mutator events but as per google, it has compatibility issues and not to use.

How about this:

 $("#btnId").on('click', function(e){ //alert(1112); var target = $(this).closest(".someclass").find(".ui-sortable"); target.append('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>'); }); $(document).on('DOMNodeInserted', function(e) { //check if li inserted to ul if(e.target.localName == "li"){ //do somethings! console.log("Inserted a li for ul!"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="someclass" id="someid"> <div class="form-someclass1 class2 "> <div class="button action right addbutton" id="btnId"><i class="add"></i> Add</div> </div> <ul class="ui-sortable"> <!-- dynamically li gets added. --> </ul> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM