为什么html()执行JavaScript,但innerHTML不执行?

为什么这会执行< script>:
$('#jq_script').html("<script>$('#test').text('test');<\/script>");

但这不是吗?

document.getElementById('js_script').innerHTML = "<script>$('#test').text('test');<\/script>";

你可以在行动here中看到它

来自jQuery的关于.html()的文档:

This method uses the browser’s innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document. For example,Internet Explorer sometimes leaves off the quotes around attribute values if they contain only alphanumeric characters.

解决方法

html是一个jQuery函数. innerHTML是一种非标准(但受到良好支持)的属性.

如果你看一下代码,你会看到.html()解析脚本,然后逐出它们.

要在源中找到它:

找到html声明:https://github.com/jquery/jquery/blob/1.11.0/src/manipulation.js#L564-604

看它确实.append.依次调用DomManip [ulate]来解析和评估脚本.

DomManip [ulate]中的相关位:https://github.com/jquery/jquery/blob/1.11.0/src/manipulation.js#L684-709

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些