开发者

javascript grabbing span tag with both class and id

I have a span tag

<span class="vi-is1-prcp" id="v4-25">US $99.00</span>

I would like to grab it using pure javascript. JQuery or any other library is not allowed. Is that possible?

I recon that

getElementById('v4-25')

won't work since I have to specify class, too, correct?

Thank you,

So,

<div id="listprice">asdasdasdasdasd</div>
var string = document.getElementById('v4-25');
document.getElementById('listprice').innerHTML = string;

sho开发者_Python百科uld print value of 'v4-25' in 'listpirce' ?

H


getElementById will work just fine. Just make sure you're running it after the page has loaded.


First of all, ids are unique. You can't have more than one. therefore, when you select element by id, you can only bring back one element (this is good).

Secondly, after you get an element, you have to do something with it. var string = document.getElementById('v4-25'); only gets you the element, but it looks like you want var string = document.getElementById('v4-25').innerHTML; for the price. If you do want the id instead you can do var string = document.getElementById('v4-25').id; but because that just returns "v4-25" it's a bit redundant.


There is no reason to add a class. Run the script after that dom element is loaded like this.

<span class="vi-is1-prcp" id="v4-25">US $99.00</span>
<script>
    var elm = document.getElementById('v4-25');
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜