简体   繁体   中英

JavaScript: Registrering mouse event over a div element

My first start into JavaScript. I can make mouseover events trigger changes when I use it with images, but not with a div only containing text. What am I doing wrong ? Code follows.

<html>
    <head>
      <script language="javascript" type="text/javascript">
        function addHeading(node, text) {
          var newNode=document.createElement("h1");
          var text=document.createTextNode(text);
          newNode.appendChild(text);
          node.appendChild(newNode);
        }
      </script>
    </head>

    <body>
      <div onmouseover="addHeading(document.body, "Header added")">
      Hola!
      </div>
    </body>
</html>

It should be (note the apostrophes):

<div onmouseover="addHeading(document.body, 'Header added')">

otherwise you close the attribute value prematurely. Having that said, it works for me .

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