Why is it that script will run from using jquery's html but not from using innerHTML?

后端 未结 1 1493
醉话见心
醉话见心 2021-01-21 16:36

So I am a little confounded here. I do not understand why script executes when placed with jquery\'s .html() but not with innerHTML. I looked at the so

1条回答
  •  一整个雨季
    2021-01-21 17:03

    As far as I can tell from a bit of breakpointin', jQuery will try to use innerHTML, but, failing that, it'll fall back to emptying the target element, and appending the new content via the DOM.

    Update: see Felix Kling's comment below; basically, if the string content to be inserted contains script or style/link tags, jQuery won't use straight innerHTML

    When that happens - and it does in your case - the content goes through a function called domManip which I don't claim to fully understand, but it does specifically look for and evaluate scripts.

    So the short answer is: jQuery's not using innerHTML after all. Wish I could give you a better step-by-step of exactly what happens, but I don't know my way around jQuery's source well enough. I just set a breakpoint (at line 6074 in jQuery-1.9.1.js; the point where jQuery decides not to use innerHTML after all) and stepped around the source a little.

    0 讨论(0)
提交回复
热议问题