开发者

event listener in jquery for variable change

I want to track a variable as I navigate around my site. How can I use jQuery to output this value without having to worry about tracking down wherever it might be affected and having it update.

var num = 1;

$(#trackvariable).text(num)  

I want the above to execute constantly as it we开发者_JAVA技巧re.. I don't necessarily want to do it on a timer. Is there any way to do that?


If you want to listen to changes on a textnode, you can do something like this:

function InitListener () {
    var container = document.getElementById ("textContainer");
    var textNode = container.firstChild;
    if (textNode.addEventListener) {
        textNode.addEventListener ('DOMCharacterDataModified', OnCharacterModified, false);
    }
}


This will only work in Firefox (and probably Chrome in Safari also):

https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects#Defining_Getters_and_Setters


Off the top of my head I would say that the functions changing the variable should just call some sort of update function at the end of them? just an idea. I think even if you set up some kind of custom event it would still have to manually triggered in each function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜