最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript to change li tag text - Stack Overflow

programmeradmin5浏览0评论

I am using the following 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 Title part to something else.

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?

I am using the following 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 Title part to something else.

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?

Share Improve this question asked Sep 12, 2012 at 4:41 AaronAaron 3,64913 gold badges36 silver badges49 bronze badges 6
  • innerHTML should work just fine jsbin.com/abaxuw/1/edit – elclanrs Commented Sep 12, 2012 at 4:44
  • Could you paste the complete code that has both HTML and JS. I think somehow you are running JS before DOM is ready. – Shiplu Mokaddim Commented Sep 12, 2012 at 4:45
  • 1 @shiplu.mokadd.im Could also be duplicate IDs, but that's just another guess – Ashley Strout Commented Sep 12, 2012 at 4:46
  • @D.Strout right. In fact nothing can be said without complete reproducible source. – Shiplu Mokaddim Commented Sep 12, 2012 at 4:46
  • <ul> <li id="item1" data="url: 'reader.htm', target: 'AccessPage', reader: '1'">Reader 1</li> </ul> I am running the following code in a Function that is called as soon as the page loads. I know the function is running as I have added a Alert box to confirm. – Aaron Commented Sep 12, 2012 at 4:55
 |  Show 1 more comment

2 Answers 2

Reset to default 14

try this

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

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 '); 
 }
发布评论

评论列表(0)

  1. 暂无评论