在$(window).load()函数中运行jQuery,但不在$(document).ready函数中运行jQuery [英] Running jQuery inside $(window).load() function but not inside $(document).ready function

查看:81
本文介绍了在$(window).load()函数中运行jQuery,但不在$(document).ready函数中运行jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个现有功能,它将使用jQuery UI位置插件在网页中显示图标:

I have this existing function that will icons in a web page using jQuery UI position plugin:

<script type='text/javascript'>
jQuery( document ).ready( function( $ ) {
var element_selector='.test';
if ( $(element_selector).length !== 0) {

//jQuery UI Position code here

}

 });
</script>

此功能在HTML的页脚部分附近找到,尽管在某些地方它输出在顶部部分.有人建议我在$(window).load()函数中加载此jQuery.原因是在加载DOM时会触发$(document).ready事件,因此在文档结构就绪后但在加载图像之前会触发该事件.

This function is found near the footer section of the HTML although in some places it outputs on the head section. Someone suggested me to load this jQuery inside $(window).load() function. The reason is that $(document).ready event is fired when DOM is loaded, so it’s fired when the document structure is ready, but before images are loaded.

$(window).load()事件.因此,.position()方法在发射到较早(加载图片之前)时可能无法计算正确的位置.

$(window).load() event is fired after whole content is loaded. So .position() method might not calculate correct position when it’s fired to early (before images are loaded).

如果为true,则如何修改$(window).load()中的上述负载.谢谢.

If this true, how to revise the above load in $(window).load(). Thanks.

更新:我从以下位置更改:

UPDATE: I have changed from:

jQuery( document ).ready( function( $ ) {

对此:

jQuery(window).load(function($) {

然后我在控制台中收到此错误:

And I get this error in the console:

Uncaught TypeError: object is not a function 

此行失败:

$(divname515e62e8355b0).children().wrapAll('<div class="mydoc cf">');

其中divname515e62e8355b0是在此处定义的选择器:

where divname515e62e8355b0 is the selector define here:

var divname515e62e8355b0 = '#test_selector';

推荐答案

jQuery(window).load(function($) {
    var element_selector='.test';
    if ( $(element_selector).length !== 0) {
        // Do stuff
    }
});

这篇关于在$(window).load()函数中运行jQuery,但不在$(document).ready函数中运行jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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