开发者

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

开发者 https://www.devze.com 2023-01-29 06:31 出处:网络
I have a span tag without an 开发者_Python百科id eg: <span>Welcome</span> I have many span tags in the same web page.

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

精彩评论

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