如何在Javascript中监视UL元素上的元素更改 [英] How to monitor element changes on a UL element in Javascript

查看:47
本文介绍了如何在Javascript中监视UL元素上的元素更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视UL元素的子元素(例如添加或删除的li等)的变化.UL用于在屏幕上显示选项卡,其中每个li将是一个单独的选项卡.当标签被改变了我就不用管了修改标签脚本打电话给我自己的功能在我的脚本文件.

为此,我必须主要支持IE浏览器,并且必须使用纯JavaScript.

解决方案

我知道唯一可行的方法就是这样.我不能说这是一个很好的方法.

  var onchange = function(element,callback){var HTML = element.innerHTML;window.setInterval(function(){var newHTML = element.innerHTML;if(HTML!== newHTML){HTML = newHTML;回调(元素);}});} 

这将监视元素并检查 innerHTML 是否已更改,它将调用您的回调函数.

演示: http://jsfiddle.net/qmB97/

用法:

  onchange(HTMLElement,function(){alert('change');}); 

I want to monitor a UL element for the changes in child elements such as li added or removed etc. The UL is used to display tabs in the screen where each li will be a separate tab. I don't have control to modify the tab script to call my own functions in my script file when the tabs are changed.

I have to support IE browser mainly for this and have to do it using pure JavaScript.

解决方案

The only way i know for this to be possible would be like this. I cannot say that this is a very good way to do it though.

var onchange = function(element, callback) {
    var HTML = element.innerHTML;
    window.setInterval(function() {
        var newHTML = element.innerHTML;
        if(HTML !== newHTML) {
            HTML = newHTML;
            callback(element);
        }
    });
}

This will monitor the element and check the innerHTML if it has changed it will call your callback function.

Demo: http://jsfiddle.net/qmB97/

Usage:

onchange(HTMLElement, function() {
   alert('change');
});

这篇关于如何在Javascript中监视UL元素上的元素更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆