jQuery代码仅适用于页面刷新 [英] jQuery code only works on page refresh

查看:101
本文介绍了jQuery代码仅适用于页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的jQuery脚本,它可以更改动态拉出的值,如下所示:

I have a simple jQuery script that changes a value pulled dynamically like so:

(function($) {

    $(window).load(function () {
        $(function(){
            var inNum = parseFloat($(".value").text().match(/[\d\.\d]+/i)).toFixed(2);
            $('span.value').text(inNum);
        });
    });
}(jQuery));

但是,该脚本仅在刷新页面后才起作用.我尝试使用

However the script only works when the page is refreshed. I have tried using

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

但仍然没有运气.该脚本需要在另外两个脚本完成后才能运行,我认为这可能是导致问题的原因,但是刷新始终可以解决该问题.还有其他原因导致这种情况发生的原因吗?除了使用.load之外,是否可以将脚本设置为在其他所有内容加载之后加载?

but still no luck. The script needs to run once two other scripts have completed which I think could be causing the problem, but a refresh always fixes the issue. Are there any other reasons as to why this is happening? Can I set the script to load after everything else has loaded other than using .load?

推荐答案

您的函数无法正常工作,可能是因为某些资源(脚本,css或图像)仍在加载,因此仅当您单击刷新时才会触发load事件这将导致所有资源停止加载.这会导致在实际页面中触发load事件.

Your function isn't working probably because some resource (script, css or image) still loading, so the load event is only fired when you hit refresh which causes all resources to stop loading. It causes the load event to be fired in the actual page.

您可以使用:

(function($) {
    $(function(){
        var inNum = parseFloat($(".value").text().match(/[\d\.\d]+/i)).toFixed(2);
        $('span.value').text(inNum);
    });
}(jQuery));

jQuery变量仍将映射到函数内部的$,并且在DOM准备就绪时将执行脚本(即使某些资源仍在加载中).

The jQuery variable still be mapped to $ inside your function and your script will be executed when the DOM is ready (even if some resource still loading).

这篇关于jQuery代码仅适用于页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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