简体   繁体   English

为什么我的jquery代码未执行?

[英]why is my jquery code not being executed?

Going slightly insane here, I'm wondering why a simple 在这里有点疯狂,我想知道为什么一个简单的

console.log("test")

isn't appearing in my console. 没有出现在我的控制台中。 My code is below. 我的代码如下。 I also tried 我也试过

$(function() {
   console.log("test")
});

but that didn't do it either. 但这也没有做到。 Isn't the purpose of the above and my function below to wait until the page loads and then do the function? 上面的功能和下面的功能的目的不是要等到页面加载然后执行该功能吗? No matter where I put the code - header, body...it's not working. 无论我将代码放在哪里-标头,正文...都无法正常工作。

<!DOCTYPE html>
<html>
<head>
    <title>jQM Autocomplete</title>
    <meta content="initial-scale=1, maximum-scale=1, user-scalable=0" name="viewport" />
    <meta name="viewport" content="width=device-width" />
    <meta name="apple-mobile-web-app-capable" content="yes" />

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="styles.css" />

    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
    <script src="jqm.autoComplete-1.5.2.js"></script>
    <script src="code.js"></script>
</head>

<body>

    <div data-role="page" id="mainPage">

        <div data-role="content">

            <p>
                In this example autoComplete uses a local array comprised of strings. This also shows an example of the matchFromStart property set to false.
            </p>


            <p>
                <input type="text" id="searchField" placeholder="Categories">
                <ul id="suggestions" data-role="listview" data-inset="true"></ul>
            </p>

        </div>

    </div>

    <script>


    $(document).on("pageshow", "mainPage", function() {
   console.log("test")
});

    </script>

</body>
</html>

The console is ready way before the page so you won't get anything by using document.ready callback. 在页面之前,控制台已准备就绪,因此您不会通过使用document.ready回调获得任何信息。 Chances are something has overridden your console. 可能是某些东西已覆盖您的控制台。 To find out if that's true, inspect your console object by simply typing console into your console. 要确定是否正确,只需在控制台中键入console即可检查控制台对象。 In Chrome, it should return something like this: 在Chrome中,它应返回如下内容:

Console {memory: MemoryInfo, debug: function, error: function, info: function, log: function…}

If that proves true, you need to find where in your code is the console redefined. 如果证明是正确的,则需要找到在代码中重新定义控制台的位置。 Also, you might want to test your code in another browser - perhaps you have an extension or something that's blocking the console for some reason. 另外,您可能想在其他浏览器中测试代码-也许您有扩展名或由于某种原因而阻塞了控制台。

But before everything, please double-check your code for syntax errors. 但在进行所有操作之前,请仔细检查您的代码是否存在语法错误。

Also, make sure you've set your console to show logs (the filter button, it's next to the clear console button). 另外,请确保已将控制台设置为显示日志(过滤器按钮,它位于清除控制台按钮旁边)。

The problem was to do with my console settings. 问题出在我的控制台设置上。 In console there's some grey buttons along the bottom - All, Errors, Warnings, Logs, Debug. 在控制台中,底部有一些灰色按钮-全部,错误,警告,日志,调试。 Well, Errors was selected, for some reason. 好吧,由于某种原因选择了错误。 So I was getting nothing in Console but errors. 所以我在控制台中什么也没得到,只有错误。 Click 'All' and it works properly. 单击“全部”,它可以正常工作。

Having said that, my code was also missing that # before mainpage, as highlighted by null. 话虽如此,我的代码也缺少在主页之前的#号(以null突出显示)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM