javascript写入_如何在JavaScript中写入HTML元素?

本文介绍如何使用JavaScript将文本写入HTML元素。通过获取HTML元素的ID,并使用innerHTML属性,可以轻松地更新页面上的文本内容。示例展示了在两个段落元素中分别写入文本和计算结果。

javascript写入

写入HTML元素 (Writing into an HTML element)

To write string/text into an HTML element, we use the following things:

要将字符串/文本写入HTML元素,我们使用以下内容:

  • There must be an HTML element like paragraph, span, div etc.

    必须有一个HTML元素,例如段落,跨度,div等。

  • An HTML element must have an Id.

    HTML元素必须具有ID。

  • We access the HTML element by using the id (to access an HTML element using Id, we use an HTML DOM method getElementbyId()).

    我们使用id访问HTML元素(要使用Id访问HTML元素,我们使用HTML DOM方法getElementbyId() )。

  • Then, we write the text to the HTML element by using an HTML DOM property innerHTML.

    然后,我们使用HTML DOM属性innerHTML将文本写入HTML元素。

Example:

例:

In this example, we have two paragraphs with p1 and p2 ids, we are writing two different texts in these HTML elements (paragraphs).

在此示例中,我们有两个具有p1和p2 id的段落,我们在这些HTML元素(段落)中编写了两个不同的文本。

Example:

例:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<p id="p1"></p>
	<p id="p2"></p>
	<script>
		document.getElementById("p1").innerHTML = "Hello world!";
		document.getElementById("p2").innerHTML = 10+20;
	</script>
</body>
</html>

Output

输出量

Hello world!

30


翻译自: https://www.includehelp.com/code-snippets/write-into-an-HTML-element-in-javascript.aspx

javascript写入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值