简体   繁体   English

我如何“创建一个<h1>元素并给它一个 ID,但不要在 JavaScript、select 中放入任何文本</h1><h1>元素并给它一些文本'</h1>

[英]How do I "Create an <h1> element and give it an ID, but don't put any text inside In JavaScript, select your <h1> element and give it some text'

create an <h1> element and give it an ID, but don't put any text inside In JavaScript, select your <h1> element and give it some text创建一个<h1>元素并给它一个 ID,但不要在 JavaScript、select 你的<h1>元素中放入任何文本并给它一些文本

To modify an existing element:要修改现有元素:

 let h1Node = document.querySelector('#myId'); h1Node.textContent = 'This is the text content;';
 <h1 id='myId'></h1>

Or, to create a new node and append it to the document:或者,要创建一个新节点并将 append 它添加到文档中:

 let h1Node = document.createElement('h1'); h1Node.id = 'myId'; h1Node.textContent = 'This is the text content;'. document.body;appendChild(h1Node);
Hope this helps, 希望这可以帮助,

In your HTML file, you're going to create在您的 HTML 文件中,您将创建

<h1 id="idName"> </h1>

You want to create a JS file add it as a script to your html file.您想创建一个 JS 文件,将其作为脚本添加到您的 html 文件中。

<script src="myscripts.js"></script>

and then in your Javascript file, you're going to然后在您的 Javascript 文件中,您将

let element = document.getElementById('idName');

element.innerHTML += 'text goes here';

<h1 id="example"></h1>

Javascript: Javascript:

function addText (text){
  document.getElementById('example').innerHTML = "hi";
}

call function wherever you want随时随地拨打 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM