最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - Run a function after .load() is complete AND DOM is updated - Stack Overflow

programmeradmin0浏览0评论

I'm trying to run a function on an object that is loaded into the DOM when the user clicks on the menu. Here is some part of my code:

$('#menu li a').click(function(){  
            var toLoad = $(this).attr('href');
            //...

            var $newItem = $('<li></li>').appendTo($(columns).first());

            loadContent();
            function loadContent() {
                $newItem.load(toLoad, runScript());
            }

            function runScript() {
                //see the code in IE9's debugger
                alert($newItem.get(0).outerHTML);
                obj.doWidget($newItem.get(0));
            }

            return false;
        });

The problem is, when I click on the menu item, the alert function in the above code, shows the code before loading: <li></li> and not the loaded element from the object itself hence the script changes the old object and the new object overwrites its content. (like I've never ran the script). I assumed that the fallback function should implement return the loaded object. How can I get that object? Thanks.

I'm trying to run a function on an object that is loaded into the DOM when the user clicks on the menu. Here is some part of my code:

$('#menu li a').click(function(){  
            var toLoad = $(this).attr('href');
            //...

            var $newItem = $('<li></li>').appendTo($(columns).first());

            loadContent();
            function loadContent() {
                $newItem.load(toLoad, runScript());
            }

            function runScript() {
                //see the code in IE9's debugger
                alert($newItem.get(0).outerHTML);
                obj.doWidget($newItem.get(0));
            }

            return false;
        });

The problem is, when I click on the menu item, the alert function in the above code, shows the code before loading: <li></li> and not the loaded element from the object itself hence the script changes the old object and the new object overwrites its content. (like I've never ran the script). I assumed that the fallback function should implement return the loaded object. How can I get that object? Thanks.

Share Improve this question asked Nov 12, 2011 at 16:48 Alireza NooriAlireza Noori 15.3k30 gold badges102 silver badges190 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Pass a reference to runScript not the results of executing the function:

$newItem.load(toLoad, runScript);
发布评论

评论列表(0)

  1. 暂无评论