Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery&HTML:脚本可以从控制台工作,但不能从文件或_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript jQuery&HTML:脚本可以从控制台工作,但不能从文件或

Javascript jQuery&HTML:脚本可以从控制台工作,但不能从文件或,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个网站的脚本,如果从移动设备查看页面,格式和样式会发生变化。在站点的2/3页面上,脚本非常有效,正如我所希望的那样。但是,在最后一个脚本中,用于更改格式和样式的脚本运行,但不是完全运行。我尝试在.html文档的结尾和开头使用脚本标记运行代码,但没有任何变化。代码是: <script> if( jQuery.browser.mobile == true ) { alert(true); $("#siteHeader").css({

我有一个网站的脚本,如果从移动设备查看页面,格式和样式会发生变化。在站点的2/3页面上,脚本非常有效,正如我所希望的那样。但是,在最后一个脚本中,用于更改格式和样式的脚本运行,但不是完全运行。我尝试在.html文档的结尾和开头使用脚本标记运行代码,但没有任何变化。代码是:

<script>    
    if( jQuery.browser.mobile == true ) {
        alert(true);

        $("#siteHeader").css({
            "position": "absolute",
            "width": $(window).innerWidth + 10 + "px",
        });

        $("#mainContent").css({
            "top": "898px",
        });

        $("#queensTitle").css({
            "display": "none",
        });

        $("#pageTitle").css({
            "text-align": "left",
            "width": "100%",
        });

        $("#pageTitleText").css({
            "width": "93%",
            "margin-left": "20px",
        });

        $("#siteMenu").css({
            "padding-left": "0px",
            "padding-top": "7px",
            "height": "initial",
            "margin-top": "initial",
            "text-align": "center",
            "border-bottom": "28px solid #063e53",
        });

        $(".menuItems").css({
            "width": "100%",
            "height": "initial",
            "float": "initial",
            "margin": "auto",
            "font-size": "5em",
            "font-weight": "700",
            "padding": "0px",
        });

        $("body").width($("#siteMenu").width() - 15).css({
        });

        if ($(location).attr("pathname") == "/drama/about-us-template.html") {
            $("#aboutUsDesc").css({
                "font-size": "1.7em",
            });

            $("#backgroundImageAU").height(247 + $("#aboutUsTitleContainer").height() + $("#aboutUsDescContainer").height() + 21);
        };
    };
</script>
siteHeader格式已更改,主要内容已更改,但其他内容不变。但是,如果我通过控制台运行上述代码,或者在运行页面时按CTRL-F5,则所有代码都会按照我的要求工作。这里可能有什么问题

另外,我搜索了stackoverflow,发现了标题类似但场景完全不同的问题,我尝试了他们的解决方案,但没有结果


是我网站的链接。我正在使用Google Chrome开发者工具设备仿真和我自己的手机来测试移动视图。

如果jQuery的各个部分在DOM中找不到元素,那么它将抛出异常,脚本将不再执行

$("#queensTitle")  => undefined
然后打电话

$("#queensTitle").css({ ... });
将导致脚本停止执行,并且在此点之后不会执行任何其他内容。 在尝试设置CSS之前,您应该尝试检查元素

var elem = $("#queensTitle");
if ( elem ) {
   elem.css({ "display": "none" })
}
请注意,在语句末尾和结束“}”之间不需要额外的“,”at,因为某些浏览器会引发异常,而解析器需要额外的语句


运行脚本时,浏览器控制台中是否报告了任何错误或异常

为什么不使用CSS媒体查询呢?嗯,我怎么才能只选择手机呢?我知道可以为媒体查询指定宽度,但手机呢?不,我没有收到任何错误。我已经编辑了我的脚本,所以我只寻找已经存在的元素。现在一切都很好,谢谢!