简体   繁体   中英

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. 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. In Chrome, it should return something like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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