简体   繁体   中英

javascript alert message does not pop up, why?

this should work, I am just trying to contact a node and find out it's value.

<html>
    <head></head>
    <body>
        <script type="text/javascript">
            function nadfunc() {
                var demoList = document.getElementById('eventsList');
                alert(var);
            }
            nadfunc();
        </script>

        <ul id="eventsList">
            <li>List</li>
            <li>List</li>
            <li>
                <a id="linkedItem" href="http://www.google.com">Linked List Item</a>
            </li>
            <li>List</li>
        </ul>
    </body>
</html>

write

alert(demoList);

var is just a keyword, you have to use actual variable name.

Also, Your script block should be placed at the end of the page before closing body tag:

<html>
    <head></head>
    <body>    
        <ul id="eventsList">
            <li>List</li>
            <li>List</li>
            <li><a id="linkedItem" href="http://www.google.com">Linked List Item</a></li>
            <li>List</li>
        </ul>

        <script type="text/javascript">
            function nadfunc() {
                var demoList = document.getElementById('eventsList');
                alert(demoList);
            }
            nadfunc();
        </script>
    </body>
</html>

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