用于更改 li 标签文本的 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12381326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:51:10  来源:igfitidea点击:

Javascript to change li tag text

javascripthtmlhtml-lists

提问by Aaron

I am using the following HTML:

我正在使用以下 HTML:

<li id="item1" data="icon: 'base.gif', url: 'page.htm', target: 'Page'">Item 1 Title</li>

I am trying to change the Item 1 Titlepart to something else.

我正在尝试将Item 1 Title部分更改为其他内容。

I have tried using:

我试过使用:

document.getElementById("item1").innerHTML = "Item was changed";

but had no luck. Even tried using:

但没有运气。甚至尝试使用:

document.getElementById("item1").value = "Item was changed";

but still no luck.

但仍然没有运气。

I am only wanting to change the text and leave eveything else the same.

我只想更改文本并保持其他一切不变。

Am I doing something wrong?

难道我做错了什么?

回答by Nudier Mena

try this

尝试这个

window.onload = function(){
 document.getElementById("item1").innerHTML = "Item was changed";
}

回答by Abraham Adam

you can make a function and all it after the page is loaded

您可以在页面加载后创建一个功能和所有功能

var changeText = function(text){ 
    document.getElementById("item1").innerHTML = text;
}

window.onload = function() { 
    changeText(' some text '); 
 }