How do I clear inner HTML

前端 未结 4 1748
盖世英雄少女心
盖世英雄少女心 2021-02-03 23:03

I\'ve been fiddling with this for a while but it won\'t work and I can\'t figure out why. Please help. Here is what I have:



    

        
相关标签:
4条回答
  • 2021-02-03 23:19

    The h1 tags unfortunately do not receive the onmouseout events.

    The simple Javascript snippet below will work for all elements and uses only 1 mouse event.

    Note: "The borders in the snippet are applied to provide a visual demarcation of the elements."

    document.body.onmousemove = function(){ move("The dog is in its shed"); };
    
    document.body.style.border = "2px solid red";
    document.getElementById("h1Tag").style.border = "2px solid blue";
    
    function move(what) {
        if(event.target.id == "h1Tag"){ document.getElementById("goy").innerHTML = "what"; } else { document.getElementById("goy").innerHTML = ""; }
    }
    <h1 id="h1Tag">lalala</h1>
    <div id="goy"></div>

    This can also be done in pure CSS by adding the hover selector css property to the h1 tag.

    0 讨论(0)
  • 2021-02-03 23:24

    Take a look at this. a clean and simple solution using jQuery.

    http://jsfiddle.net/ma2Yd/

        <h1 onmouseover="go('The dog is in its shed')" onmouseout="clear()">lalala</h1>
        <div id="goy"></div>
    
    
        <script type="text/javascript">
    
        $(function() {
           $("h1").on('mouseover', function() {
              $("#goy").text('The dog is in its shed');
           }).on('mouseout', function() {
              $("#goy").text("");
           });
       });
    
    0 讨论(0)
  • 2021-02-03 23:33

    The problem appears to be that the global symbol clear is already in use and your function doesn't succeed in overriding it. If you change that name to something else (I used blah), it works just fine:

    Live: Version using clear which fails | Version using blah which works

    <html>
    <head>
        <title>lala</title>
    </head>
    <body>
        <h1 onmouseover="go('The dog is in its shed')" onmouseout="blah()">lalala</h1>
        <div id="goy"></div>
        <script type="text/javascript">
        function go(what) {
            document.getElementById("goy").innerHTML = what;
        }
        function blah() {
            document.getElementById("goy").innerHTML = "";
        }
        </script>
    </body>
    </html>
    

    This is a great illustration of the fundamental principal: Avoid global variables wherever possible. The global namespace in browsers is incredibly crowded, and when conflicts occur, you get weird bugs like this.

    A corollary to that is to not use old-style onxyz=... attributes to hook up event handlers, because they require globals. Instead, at least use code to hook things up: Live Copy

    <html>
    <head>
        <title>lala</title>
    </head>
    <body>
        <h1 id="the-header">lalala</h1>
        <div id="goy"></div>
        <script type="text/javascript">
          // Scoping function makes the declarations within
          // it *not* globals
          (function(){
            var header = document.getElementById("the-header");
            header.onmouseover = function() {
              go('The dog is in its shed');
            };
            header.onmouseout = clear;
    
            function go(what) {
              document.getElementById("goy").innerHTML = what;
            }
            function clear() {
              document.getElementById("goy").innerHTML = "";
            }
          })();
        </script>
    </body>
    </html>
    

    ...and even better, use DOM2's addEventListener (or attachEvent on IE8 and earlier) so you can have multiple handlers for an event on an element.

    0 讨论(0)
  • 2021-02-03 23:36
    const destroy = container => {
      document.getElementById(container).innerHTML = '';
    };
    

    Faster previous

    const destroyFast = container => {
      const el = document.getElementById(container);
      while (el.firstChild) el.removeChild(el.firstChild);
    };
    
    0 讨论(0)
提交回复
热议问题