如果人们建议我不应该使用.innerHTML,那我应该使用什么呢? [英] If people recommend I shouldn't use .innerHTML, what then should I instead use?

查看:96
本文介绍了如果人们建议我不应该使用.innerHTML,那我应该使用什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JavaScript:是吗?最好使用innerHTML或(很多)createElement调用来添加复杂的div结构?

我应该使用什么代替 .innerHTML ?感谢所有建议.

What should I use instead of .innerHTML? All advice is appreciated.

推荐答案

正确的答案是,在某些情况下,您应该使用innerHTML,在其他情况下,您应该使用appendChild.以下是使用innerHTML或appendChild的时间:

The correct answer is that in certain situations you should use innerHTML, and in other situations you should use appendChild. Here's when to use innerHTML or appendChild:

  • 在HTML标记(例如锚标记,段落标记,span,div或textarea)中设置文本时,请使用innerHTML.

  • Use innerHTML when you're setting text inside of an HTML tag like an anchor tag, paragraph tag, span, div, or textarea.

使用appendChild()如果要在另一个DOM内添加新的DOM元素元素.

Use appendChild() If you're trying to add new DOM elements inside of another DOM element.

这篇关于如果人们建议我不应该使用.innerHTML,那我应该使用什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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