javascript 使用javascript从itemprop读取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9627756/
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-10-26 07:15:54 来源:igfitidea点击:
using javascript to read value from itemprop
提问by jbcedge
Is it possible the read the price using the javascript
是否可以使用 javascript 读取价格
<span id="product_price_00873">
<span itemprop="price">178.00</span>
</span>
I just want to price 178.00. I can only using javascript.
我只想定价 178.00。我只能使用javascript。
Any suggestions will be appreciated.
任何建议将不胜感激。
回答by icktoofay
If you have the product element in product
and you're using a modern browser, this should work:
如果您有产品元素product
并且您使用的是现代浏览器,这应该可以工作:
var price = product.querySelector('[itemprop=price]').textContent;
回答by xandercoded
var els = document.getElementsByTagName('span'), i = 0, price;
for(i; i < els.length; i++) {
prop = els[i].getAttribute('itemprop');
if(prop) {
price = els[i].innerHTML;
break;
}
}