开发者

Change Text b/n Span Tag with has no id, in java script

I have a span tag without an 开发者_Python百科id

eg: <span>Welcome</span>

I have many span tags in the same web page.

Is there a way to access this span tag in the javascript and change the

"Welcome" to "something else"

Thanks


If there's no ID or other attribute you can select, then you can loop through all <span> elements looking for the exact text (via .innerHTML for example), like this:

var spans = document.getElementsByTagName("span");
for(var i=0;i<spans.length; i++) {
    if(spans[i].innerHTML == "Welcome") {       //is this the "Welcome" span?
        spans[i].innerHTML = "something else";  //change to new value
        break;                                  //hop out of the loop, we're done
    }
}

You can test it out here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜