JQuery html() 与 innerHTML
我可以完全依赖 jQuery 的 html()
方法,其行为与 innerHTML
相同吗? innerHTML
和 jQuery 的 html()
方法有什么区别吗?如果这些方法的作用相同,我可以使用 jQuery 的 html()
方法来代替 innerHTML
吗?
我的问题是:我正在处理已经设计的页面,这些页面包含表格,并且在 JavaScript 中,innerHTML
属性用于动态填充它们。
该应用程序在 Firefox 上运行良好,但 Internet Explorer 引发错误:未知的运行时异常
。我使用了 jQuery 的 html()
方法,IE 的错误消失了。但我不确定它是否适用于所有浏览器,并且我不确定是否用 jQuery 的 html()
方法替换所有 innerHTML
属性。
多谢。
Can I completely rely upon jQuery's html()
method behaving identical to innerHTML
? Is there any difference between innerHTML
and jQuery's html()
method? If these methods both do the same, can I use jQuery's html()
method in place of innerHTML
?
My problem is: I am working on already designed pages, the pages contains tables and in JavaScript the innerHTML
property is being used to populate them dynamically.
The application is working fine on Firefox but Internet Explorer fires an error: unknown runtime exception
. I used jQuery's html()
method and IE's error has disappeared. But I'm not sure it will work for all browsers and I'm not sure whether to replace all innerHTML
properties with jQuery's html()
method.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
回答你的问题:
.html()
在对 nodeTypes 和其他内容进行一些检查后只会调用.innerHTML
。它还使用try/catch
块,首先尝试使用innerHTML
,如果失败,它将优雅地回退到 jQuery 的.empty() + <代码>追加()
To answer your question:
.html()
will just call.innerHTML
after doing some checks for nodeTypes and stuff. It also uses atry/catch
block where it tries to useinnerHTML
first and if that fails, it'll fallback gracefully to jQuery's.empty()
+append()
特别是关于“我可以完全依赖 jquery html() 方法吗?它会像innerHTML一样执行”,我的答案是否定的!
在 Internet Explorer 7 或 8 中运行此命令,您就会看到。
当设置包含
这里有几个测试用例,运行时的注释应该足够不言自明。这是相当晦涩的,但不理解发生了什么有点令人不安。我将提交一份错误报告。
Specifically regarding "Can I rely completely upon jquery html() method that it'll perform like innerHTML" my answer is NO!
Run this in internet explorer 7 or 8 and you'll see.
jQuery produces bad HTML when setting HTML containing a <FORM> tag nested within a <P> tag where the beginning of the string is a newline!
There are several test cases here and the comments when run should be self explanatory enough. This is quite obscure, but not understanding what's going on is a little disconcerting. I'm going to file a bug report.
如果您想了解功能,那么 jQuery 的
.html()
会执行与.innerHTML
相同的预期功能,但它还会执行以下检查:跨浏览器兼容性。因此,您始终可以尽可能使用 jQuery 的
.html()
而不是.innerHTML
。If you're wondering about functionality, then jQuery's
.html()
performs the same intended functionality as.innerHTML
, but it also performs checks for cross-browser compatibility.For this reason, you can always use jQuery's
.html()
instead of.innerHTML
where possible.鉴于目前 对
.innerHTML
的普遍支持,唯一有效的现在的区别是.html()
将执行任何标记中的代码(如果您提供的 html 中有任何代码)。
.innerHTML
,HTML5< /a>,不会。来自 jQuery 文档:
注意:
.innerHTML
和.html()
都可以通过其他方式执行js(例如onerror
属性)。Given the general support of
.innerHTML
these days, the only effective difference now is that.html()
will execute code in any<script>
tags if there are any in the html you give it..innerHTML
, under HTML5, will not.From the jQuery docs:
Note: both
.innerHTML
and.html()
can execute js other ways (e.g theonerror
attribute).innerHTML 不是标准的,可能无法在某些浏览器中工作。我在所有浏览器中使用 html() 都没有问题。
innerHTML is not standard and may not work in some browsers. I have used html() in all browsers with no problem.
“此方法使用浏览器的innerHTML 属性。” - jQuery API
http://api.jquery.com/html/
"This method uses the browser's innerHTML property." - jQuery API
http://api.jquery.com/html/
这里有一些代码可以帮助您入门。您可以修改 .innerHTML 的行为 - 您甚至可以创建自己的完整 .innerHTML shim。 (PS:重新定义 .innerHTML 也可以在 Firefox 中使用,但不能在 Chrome 中使用——他们正在努力。)
http ://jsfiddle.net/DLLbc/9/
Here is some code to get you started. You can modify the behavior of .innerHTML -- you could even create your own complete .innerHTML shim. (P.S.: redefining .innerHTML will also work in Firefox, but not Chrome -- they're working on it.)
http://jsfiddle.net/DLLbc/9/
一个关键的区别是 jQuery 的
.html()
方法执行 HTML 内的任何内联标记。
这对我来说很关键,因为我使用 fetch 请求来获取
response
,该响应的 HTML 内有标签,作为在 WordPress 中进行模态工作的一部分。它不起作用,我将其范围缩小到
.html()
与.innerHTML
我认为它们是相同的。在控制台记录响应并将其复制到文件并搜索后,我发现使用
.html()
而不是使用运行的代码>.innerHTML
。jAndy 提到还有类型检查。
要执行脚本,请检查 RedRiderX 在上述评论中链接的问题:如何在通过 ajax 加载的 html 中运行 javascript
jQuery
.html()
文档,这对于这个利基问题不是很有帮助:https://api.jquery.com/html/A key difference is that jQuery's
.html()
method executes any inline<script>
tags inside the HTML.This was key for me as I was using a fetch request to get a
response
that had HTML that had<script>
tags inside as part of making a modal work in WordPress. It wasn't working and I narrowed it down to.html()
vs.innerHTML
which I thought were identical. After console logging the response and copying it to a file and searching for<script>
I found the code that was being run with.html()
and not with.innerHTML
.There is also type checking as mentioned by jAndy.
For executing the scripts, check this question linked in the above comments by RedRiderX: how to run javascript in html loaded via ajax
jQuery
.html()
documentation, which isn't super helpful for this niche question: https://api.jquery.com/html/