繁体   English   中英

document.getElementByTag

[英]document.getElementByTag

我已经使用了document.getElementById,但是它不再起作用了。 我想要得到0,01€的值,但我不能。 我想将其保存在我的NSString *price但如何保存。 HTML代码是

<tr class="price">
<td>0,01</td>
<td>EUR</td>

我的主意是

NSString *price = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('price.td').innerText;"];

在这种情况下,tagName是'td'而不是'price.td',但是如果运行document.getElementsByTagName('id') ,则将获得页面中所有表单元格,而这可能不是您想要的。

如果页面中加载了jQuery,则可以使用jQuery('.price td:first').text()来获取价格。

如果它没有jQuery,但您可以控制页面,则可以向其中添加一个类( <td class="my-price">12.44</td> ),并使用document.getElementsByClassName('my-price')[0].innerHTML

如果您没有页面控制权,也没有jQuery,则必须找到'price'行,然后使用document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML来获取其第一个单元格。 document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML

另外,一种更清洁,更灵活的方法是使用Selectors API:

http://www.w3.org/TR/selectors-api/

document.querySelector('.price td:first').innerText

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM