Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When using DOM creation methods, it must assume that you want the structure the way you created it.</p> <p>If you want to ensure the same structure in different browsers, then yes you should always add it manually, even when JavaScript is parsing the HTML. Don't rely on the browser to insert it for you, because browsers may not behave the same in that respect.</p> <hr> <p><a href="http://jsfiddle.net/JYd7L/" rel="nofollow">Here's an example</a> you can run in Firefox.</p> <p>It starts off with this <strong><em>invalid</em></strong> markup.</p> <pre><code>&lt;p&gt; &lt;div&gt;original&lt;/div&gt; &lt;/p&gt; </code></pre> <p>The <code>&lt;div&gt;</code> gets kicked out of the <code>&lt;p&gt;</code> when the HTML is parsed, leaving it rendered like this...</p> <pre><code>&lt;p&gt;‌&lt;/p&gt; &lt;div&gt;original&lt;/div&gt; &lt;p&gt;&lt;/p&gt; </code></pre> <p>But if we create an identical structure with DOM methods, Firefox doesn't do any corrections.</p> <pre><code>var p = document.createElement('p'); p.appendChild(document.createElement('div')) .appendChild(document.createTextNode('new')); document.body.appendChild(p); </code></pre> <p>So the resulting DOM is now this...</p> <pre><code>&lt;p&gt;‌&lt;/p&gt; &lt;div&gt;original&lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt; &lt;div&gt;new&lt;/div&gt; &lt;/p&gt; </code></pre> <p>So you can see that even in the case of a completely invalid structure, it doesn't make the corrections you'd see when it parses HTML.</p> <p>Ultimately, what you should do is not rely on common browser tweaks or corrections, because there's no guarantee that they will be the same between browsers.</p> <p>Use well-structured and valid HTML, and you'll avoid problems.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload