使用innerText和innerHTML在JavaScript中处理DOM

Front-end frameworks are all the rage these days. Working with a virtual DOM, while much more efficient, can be a ton of overhead when all you need to do is tweak a few elements on an existing static page. Fortunately, JavaScript comes with some methods that make it super easy to edit an element’s text as well as the HTML inside of an element.

如今,前端框架风靡一时。 使用虚拟DOM虽然效率更高,但当您需要对现有静态页面上的几个元素进行调整时,可能会花费大量的开销。 幸运的是,JavaScript附带了一些方法,这些方法使编辑元素的文本以及元素内部HTML非常容易。

Take that React, Angular, Vue, and whatever hot new framework I failed to mention!

以React,Angular,Vue以及任何我没提到的热门新框架为例!

入门 (Getting started)

To follow along at home, all you need is a web browser and a page to manipulate.

要在家中继续学习,您只需要一个Web浏览器和一个可操纵的页面即可。

To make it easy, we can load up any web page, open up our browser’s console and override it with our own HTML document:

为了简单起见,我们可以加载任何网页,打开浏览器的控制台,并用我们自己HTML文档覆盖它:

document.getElementsByTagName('body')[0].innerHTML = '<div id="gator">Alligators rule!!</div>';

Once you hit enter, the existing page will be completely replaced with a light amount of HTML and a universal truth about everybody’s favorite reptile.

按下Enter键后,现有页面将完全替换为少量HTML和关于每个人喜欢的爬行动物的普遍真理。

编辑文字 (Editing text)

All right, so in the getting started section, we sorta jumped the gun and already used innerHTML to manipulate some elements on the page.

好的,因此在入门部分,我们有点犹豫,已经使用innerHTML来操纵页面上的某些元素。

To back it up a small bit, let’s start by changing the text of an element on a page. To do so, we’ll need to select our element and then use innerText to set the content:

要备份一点,让我们从更改页面上元素的文本开始。 为此,我们需要选择元素,然后使用innerText设置内容:

document.getElementById('gator').innerText = 'OF COURSE alligators rule!';

Not much to it!

没什么大不了的!

If you wanted to prepend text to an existing string of text, you can use innerText to get the current text and then prepend to it:

如果要将文本添加到现有文本字符串的前面,可以使用innerText获取当前文本,然后添加到它的前面:

const currentText = document.getElementById('gator').innerText;
const nextText = 'Do alligators rule? ' + currentText;
document.getElementById('gator').innerText = nextText;

Appending text is even easier, since we can use +=:

由于我们可以使用+= ,因此添加文本甚至更加容易:

document.getElementById('gator').innerText += ' This definitely true.';

编辑HTML (Editing HTML)

This is all well and good, but the moment we try to introduce HTML, like wrapping the string in <strong>, things fall apart.

这一切都很好,但是当我们尝试引入HTML时,例如将字符串包装在<strong> ,事情就崩溃了。

Since innerText works with the text contents of an element, things like HTML tags end up being shown, as if the < and > were encoded as &lt; and &gt;.

由于innerText可处理元素的文本内容,因此最终会显示HTML标记之类的内容,就像<>被编码为&lt;&gt;

No big deal though, when we want to use additional mark up with our string, all we need to do is swap out innerText for innerHTML and be on our way:

没什么大不了的,当我们想对字符串使用额外的标记时,我们所需要做的就是将innerText换成innerHTML并继续进行:

document.getElementById('gator').innerHTML = '<strong>OF COURSE</strong> alligators rule!';

And just like innerHTML we can capture the value to prepend to it, and use += to append to it:

就像innerHTML一样,我们可以捕获要附加在值前面的值,并使用+=附加到该值之后:

const currentHTML = document.getElementById('gator').innerHTML;
const nextHTML = 'Do alligators rule? ' + currentHTML;
document.getElementById('gator').innerHTML = nextHTML;

document.getElementById('gator').innerHTML += ' This definitely true.';

That’s not all, when using innerHTML we can do more than just edit the text of an element. We can also use it to add additional markup, like in the case of an HTML table:

不仅如此,当使用innerHTML我们不仅可以编辑元素的文本,还可以做更多的事情。 我们还可以使用它来添加其他标记,例如在HTML表中:

document.getElementById('gator').innerHTML = `
  <table border="1">
    <thead>
      <tr>
        <th>Reptile</th>
        <th>Awesomeness</th>
      </tr>
    </thead>
    <tbody id="tableRows">
      <tr>
        <td>Alligator</td>
        <td>9001</td>
      </tr>
    </tbody>
  </table>
`;

Subsequently, we can use innerHTML to append new rows to the table on the fly:

随后,我们可以使用innerHTML随时向表中添加新行:

document.getElementById('tableRows').innerHTML += `
  <tr>
    <td>Snake</td>
    <td>-1</td>
  </tr>
`;

结论 (Conclusion)

Sure, using innerText and innerHTML doesn’t come close to the power of a front-end framework that’s leveraging a virtual DOM, but hey, it gets the job done.

当然,使用innerTextinnerHTML不能接近利用虚拟DOM的前端框架的功能,但是,它可以完成工作。

Fortunately, the days are long gone when innerHTML was the only option available for editing elements with JavaScript. Even though we’ve come a long way, it’s always good to know these things so you don’t go throwing an entire framework at a trivial problem.

幸运的是,当innerHTML是可用于使用JavaScript编辑元素的唯一选项时,日子已经一去不复返了。 尽管我们已经走了很长一段路,但了解这些知识始终是一件好事,这样您就不会把整个框架扔到一个琐碎的问题上。

翻译自: https://www.digitalocean.com/community/tutorials/js-innertext-and-innerhtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值