I am trying to dynamically create a div tag in JavaScript and place it in an existing div tag.

This code works fine on FF, Chrome, IE9 and IE10. The code needs to work in IE7.

When setting the style property, IE7 throws the following error:

Not implemented

Here is the code:

<html>
   <head>
   </head>
   <body>
      <div id="content"> </div>
      <script> 
         var input = document.createElement('div');

         // Above code above executes fine.
         input.style = "display: block;";  // << This code triggers
                                           // the error mentioned above

         input.className = "container";
         input.innerHTML = 'Test';
         var container = document.getElementById('content');
         container.appendChild(input);
      </script>
   </body>
</html>

Please let me know if I am overlooking something here or if this code needs to be changed when running in IE7. Thank you!

有帮助吗?

解决方案

Use this instead:

input.style.display = "block";

Though I wonder why you're trying to do this at all because block is already the default display style for a <div> element.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top