javascript - Creating a new DOM element from an HTML string using built-in DOM methods or prototype -


I am trying to dynamically create an element using an HTML string. Prototype and DOM here:

  // html string var s = '& lt; Li & gt; Text & lt; / li & gt; Using both is a simple example; // DOM var el1 = document.createElement (s); // prototype var l2 = new element (s); . $ ('MyList') appendChild (EL1); . $ ('MyList') appendChild (el2);   

Both views include empty lists in the list.

I know that to use the prototype's element as a constructor, a tag name and an optional attribute parameter are required, but I can think of it in an HTML string as well.

However, "You can also specify all the attributes inside the element method by using an HTML string for method arguments." ... so I am not sure what the problem is.

And yes, I know that I can do this easily in jquery, unfortunately we are not using jquery, am I really ignoring something here?

It should be clear, but links to that MSDN article are related to an IE feature only.

Generally, the following cross-browser move is that all libraries have an HTML string ( & lt; td> s, & lt; tr & gt; s, & lt; thead & gt; s, select & gt; s and more):

 < Code> var s = '& lt; Li & gt; Text & lt; / Li & gt; '; // html string var div = document.createElement ('div'); Div.innerHTML = s; Var element = div.childNodes;   

or var element = div.firstChild if you know that you are getting a root root.

However, this feature has been built in the library-accepted method prototype of creating elements from HTML strings.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -