Simple but error-prone onclick-error __javascript

Source: Internet
Author: User
Uncaught Typeerror:cannot Set Property ' onclick ' of NULL;

a word "make sure that the DOM node rendering of the Click event is performed prior to JS." Solution

Write a paragraph of their native JS Click event, JS code is written in the home page

<script type= "Text/javascript" > var all_features = document.getElementById ("All_features");

        var all_features_down = document.getElementById ("All_features_down");
            All_features.onclick=function () {var price_content = document.getelementsbyclassname ("price_content");
            for (var i = 0; i < price_content.length i++) {price_content[i].style.display = "none";
            } All_features.style.display = "None";
        All_features_down.style.display = "block";
        }; All_features_down.onclick=function () {var price_content_down = document.getelementsbyclassname ("Price_conten
            T ");
            for (var i = 0; i < price_content_down.length i++) {price_content_down[i].style.display = ' block ';
            } all_features.style.display = "block";
        All_features_down.style.display = "None";
    }; </script>

The code is nothing to see, the key is that I clicked the DOM did not in the home page, but also the wood is rendered out, so reported the above error (although it is wrong, but the user is no sense, no impact, the user clicked on the page, so it can be normal operation).

But the programmer's cleanliness (no error):
Changed it into

function trigger_switch_up () {
            var price_content = document.getelementsbyclassname ("price_content");
            for (var i = 0; i < price_content.length i++) {
                Price_content[i].style.display = ' none ';
            }
            All_features.style.display = "None";
            All_features_down.style.display = "block";
        function () trigger_switch_down{
            var price_content_down = document.getelementsbyclassname ("price_content");
            for (var i = 0; i < price_content_down.length i++) {
                price_content_down[i].style.display = ' block ';
            }
            All_features.style.display = "block";
            All_features_down.style.display = "None";
        };
<div onclick= "Trigger_switch_down ()" >some Content</div> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.