Why does a click setting innerHTML trigger two parsing events on Chrome?

筅森魡賤 提交于 2019-12-23 07:44:52

问题


Using the Timeline in the Chrome Developer Tools, I used this small piece of code to record events through innerHTML :

<!DOCTYPE html>

<html>
<head>
  <script>
    function test(){
      var wrap = document.getElementById('wrapper');
      wrap.innerHTML = "test";
    }
  </script>
</head>

<body>
  <input type="button" value="click" onClick="test();"/>
  <div id="wrapper"></div>
</body>
</html>

And I can see that there are two parsing events fired once the test method is run :

I am using Chrome Version 23.0.1271.64 m

Is it something expected ? Is it a bug from the Chrome Developer Tools ? or is there something to improve under Chrome ?


回答1:


After a bit of playing around I would guess that this has something to do with Chrome needing to parse the string "test" and then re-parse the page, or possibly just the "wrap" element after the string has been added. innerHTML is a curious function because it allows the addition of any content whatsoever, so some validation/parsing has to occur.

It is somewhat telling that if you change your function to this :

function test() {
    var wrap = document.getElementById('wrapper');
    var newtext = document.createTextNode("test");
    wrap.appendChild(newtext);
}

...then no parse events occur at all.



来源:https://stackoverflow.com/questions/13335999/why-does-a-click-setting-innerhtml-trigger-two-parsing-events-on-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!