JQuery html() 与 innerHTML [英] JQuery html() vs. innerHTML

查看:26
本文介绍了JQuery html() 与 innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能否完全依赖 jQuery 的 html() 方法与 innerHTML 的行为相同?innerHTML 和 jQuery 的 html() 方法有什么区别吗?如果这些方法都做同样的事情,我可以使用 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?

我的问题是:我正在处理已经设计好的页面,这些页面包含表格,并且在 JavaScript 中,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.

该应用程序在 Firefox 上运行良好,但 Internet Explorer 引发错误:未知运行时异常.我使用了 jQuery 的 html() 方法,IE 的错误消失了.但我不确定它是否适用于所有浏览器,我不确定是否用 jQuery 的 html() 方法替换所有 innerHTML 属性.

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.

非常感谢.

推荐答案

回答你的问题:

.html() 在对 nodeTypes 和其他东西进行一些检查后只会调用 .innerHTML .它还使用 try/catch 块,它首先尝试使用 innerHTML,如果失败,它将优雅地回退到 jQuery 的 .empty() + append()

.html() will just call .innerHTML after doing some checks for nodeTypes and stuff. It also uses a try/catch block where it tries to use innerHTML first and if that fails, it'll fallback gracefully to jQuery's .empty() + append()

这篇关于JQuery html() 与 innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆