DOM Exception when assigning HTML entities to innerHTML

后端 未结 2 1328
不知归路
不知归路 2021-01-02 17:50

On this page http://blog.zacharyvoase.com/2010/11/11/sockets-and-nodes-i/, running the following code in javascript console will throw an Exception.

var div          


        
2条回答
  •  清酒与你
    2021-01-02 18:14

    It appears as though it is expecting that you are trying to add encoded HTML but it's detecting it as malformed.
    Does var div = document.createElement('div'); div.innerHTML = "»"; do what you need?

    Per Comment: var dv = document.createElement('div'); dv.innerHTML = "»"; document.getElementById('test').appendChild(dv);

    I got the encoded character here.

    You're right about » being valid encoded HTML. I can only guess it's a limitation of the browser.

提交回复
热议问题