GVKun编程网logo

jQuery $(document).ready和UpdatePanels?

15

在这篇文章中,我们将为您详细介绍jQuery$的内容,并且讨论关于document.ready和UpdatePanels?的相关问题。此外,我们还会涉及一些关于${document).ready()的

在这篇文章中,我们将为您详细介绍jQuery $的内容,并且讨论关于document.ready和UpdatePanels?的相关问题。此外,我们还会涉及一些关于$ {document).ready()的非jQuery等效项是什么?、$(document).ready 没有 jQuery 的等价物、$(document).ready 等同于没有 jQuery、$(document).ready等同于没有jQuery的知识,以帮助您更全面地了解这个主题。

本文目录一览:

jQuery $(document).ready和UpdatePanels?

jQuery $(document).ready和UpdatePanels?

我正在使用jQuery在UpdatePanel中的元素上附加一些鼠标悬停效果。事件绑定在中$(document).ready。例如:

$(function() {        $(''div._Foo'').bind("mouseover", function(e) {        // Do something exciting    });    });

当然,这在第一次加载页面时效果很好,但是当UpdatePanel执行部分页面更新时,它不会运行,并且鼠标悬停效果在UpdatePanel内部不再起作用。

不仅在首页加载时,而且在每次UpdatePanel触发部分页面更新时,建议在jQuery中进行接线的推荐方法是什么?我应该使用ASP.NET
ajax生命周期来代替$(document).ready吗?

答案1

小编典典

UpdatePanel完全替换更新中更新面板的内容。这意味着您订阅的那些事件不再被订阅,因为该更新面板中有新的元素。

解决此问题的方法是,在每次更新后重新订阅我需要的事件。我使用$(document).ready()初始负载,然后使用Microsoft的负载PageRequestManager(如果页面上有更新面板,则可用)重新订阅每个更新。

$(document).ready(function() {    // bind your jQuery events here initially});var prm = Sys.WebForms.PageRequestManager.getInstance();prm.add_endRequest(function() {    // re-bind your jQuery events here});

PageRequestManager是一个JavaScript对象,它是自动可用如果更新面板是在页面上。只要页面上有UpdatePanel,就不需要使用上面的代码做任何事情即可使用它。

如果需要更详细的控制,则此事件传递的参数类似于.NET事件传递参数的方式,(sender,eventArgs)因此您可以查看引发该事件的原因,并仅在需要时重新绑定。

这是来自Microsoft的文档的最新版本:msdn.microsoft.com/…/bb383810.aspx


根据您的需求,您可能有一个更好的选择是使用jQuery
.on()。这些方法比每次更新都重新订阅DOM元素更为有效。但是,在使用此方法之前,请先阅读所有文档,因为它可能会满足您的需求,也可能无法满足您的需求。有很多jQuery插件无法重构为使用.delegate().on(),因此在这种情况下,最好重新订阅。

$ {document).ready()的非jQuery等效项是什么?

$ {document).ready()的非jQuery等效项是什么?

什么是非jQuery等效项$(document).ready()

$(document).ready 没有 jQuery 的等价物

$(document).ready 没有 jQuery 的等价物

我有一个使用 的脚本$(document).ready,但它不使用 jQuery 中的任何其他内容。我想通过删除 jQuery 依赖项来减轻它。

如何在$(document).ready不使用 jQuery 的情况下实现自己的功能?我知道使用window.onload会不一样,因为window.onload在加载所有图像、帧等之后会触发。

$(document).ready 等同于没有 jQuery

$(document).ready 等同于没有 jQuery

我有一个使用 $(document).ready 的脚本,但是它不使用 jQuery 的其他任何东西。 我想通过删除 jQuery 依赖项来减轻它的负担。

如何在不使用 jQuery 的情况下实现自己的 $(document).ready 功能? 我知道使用 window.onload 会有所不同,因为在加载所有图像,框架等之后, window.onload 会触发。


#1 楼

穷人的解决方案:

var checkLoad = function() {   
    document.readyState !== "complete" ? setTimeout(checkLoad, 11) : alert("loaded!");   
};  

checkLoad();  

查看小提琴

添加了这一点,我想更好一点,自己的范围,并且非递归

(function(){
    var tId = setInterval(function() {
        if (document.readyState == "complete") onComplete()
    }, 11);
    function onComplete(){
        clearInterval(tId);    
        alert("loaded!");    
    };
})()

查看小提琴


#2 楼

这个解决方案怎么样?

// other onload attached earlier
window.onload=function() {
   alert(''test'');
};

tmpPreviousFunction=window.onload ? window.onload : null;

// our onload function
window.onload=function() {
   alert(''another message'');

   // execute previous one
   if (tmpPreviousFunction) tmpPreviousFunction();
};

#3 楼

jQuery 答案对我来说非常有用。 只需一点重构,就可以很好地满足我的需求。 我希望它对其他人有帮助。

function onReady ( callback ){
    var addListener = document.addEventListener || document.attachEvent,
        removeListener =  document.removeEventListener || document.detachEvent
        eventName = document.addEventListener ? "DOMContentLoaded" : "onreadystatechange"

    addListener.call(document, eventName, function(){
        removeListener( eventName, arguments.callee, false )
        callback()
    }, false )
}

#4 楼

只需将其添加到 HTML 页面的底部...

<script>
    Your_Function();
</script>

因为,HTML 文档是由上至下解析的。


#5 楼

<script>/*JavaScript code*/</script> 放在结束 </body> 标记之前。

诚然,这可能不适合每个人的目的,因为它需要更改 HTML 文件,而不是仅仅做一些 JavaScript 文件在 la document.ready ,但仍...

$(document).ready等同于没有jQuery

$(document).ready等同于没有jQuery

我有一个使用的脚本$(document).ready,但没有使用jQuery中的其他任何脚本。我想通过删除jQuery依赖项来减轻它的负担。

如何在$(document).ready不使用jQuery的情况下实现自己的功能?我知道使用window.onload会有所不同,因为window.onload在加载所有图像,帧等之后会触发。

答案1

小编典典

有一个基于标准的替代品,尽管IE8不DOMContentLoaded支持,但超过98%的浏览器都支持它:

document.addEventListener("DOMContentLoaded", function(event) {   //do work});

jQuery的本机功能比window.onload复杂得多,如下所示。

function bindReady(){    if ( readyBound ) return;    readyBound = true;    // Mozilla, Opera and webkit nightlies currently support this event    if ( document.addEventListener ) {        // Use the handy event callback        document.addEventListener( "DOMContentLoaded", function(){            document.removeEventListener( "DOMContentLoaded", arguments.callee, false );            jQuery.ready();        }, false );    // If IE event model is used    } else if ( document.attachEvent ) {        // ensure firing before onload,        // maybe late but safe also for iframes        document.attachEvent("onreadystatechange", function(){            if ( document.readyState === "complete" ) {                document.detachEvent( "onreadystatechange", arguments.callee );                jQuery.ready();            }        });        // If IE and not an iframe        // continually check to see if the document is ready        if ( document.documentElement.doScroll && window == window.top ) (function(){            if ( jQuery.isReady ) return;            try {                // If IE is used, use the trick by Diego Perini                // http://javascript.nwbox.com/IEContentLoaded/                document.documentElement.doScroll("left");            } catch( error ) {                setTimeout( arguments.callee, 0 );                return;            }            // and execute any waiting functions            jQuery.ready();        })();    }    // A fallback to window.onload, that will always work    jQuery.event.add( window, "load", jQuery.ready );}

关于jQuery $document.ready和UpdatePanels?的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于$ {document).ready()的非jQuery等效项是什么?、$(document).ready 没有 jQuery 的等价物、$(document).ready 等同于没有 jQuery、$(document).ready等同于没有jQuery的相关知识,请在本站寻找。

本文标签: