简体   繁体   English

document.getElementByTag

[英]document.getElementByTag

I already used document.getElementById, but it does not work any more. 我已经使用了document.getElementById,但是它不再起作用了。 I want to get the value of 0,01€ but I can not. 我想要得到0,01€的值,但我不能。 I want to save it in my NSString *price but how. 我想将其保存在我的NSString *price但如何保存。 The HTML code is HTML代码是

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

My idea was 我的主意是

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

The tagName in this case is 'td' not 'price.td', but if you run document.getElementsByTagName('id') you will get all the table cells in the page which is probably not what you want. 在这种情况下,tagName是'td'而不是'price.td',但是如果运行document.getElementsByTagName('id') ,则将获得页面中所有表单元格,而这可能不是您想要的。

If the page has jQuery loaded in it you can use jQuery('.price td:first').text() to get the price. 如果页面中加载了jQuery,则可以使用jQuery('.price td:first').text()来获取价格。

If it doesn't have jQuery but you have control of the page you can add a class to it ( <td class="my-price">12.44</td> ) and get it with document.getElementsByClassName('my-price')[0].innerHTML . 如果它没有jQuery,但您可以控制页面,则可以向其中添加一个类( <td class="my-price">12.44</td> ),并使用document.getElementsByClassName('my-price')[0].innerHTML

If you don't have control of the page and it doesn't have jQuery you will have to find the 'price' row and then get its 1-st cell by using document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML . 如果您没有页面控制权,也没有jQuery,则必须找到'price'行,然后使用document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML来获取其第一个单元格。 document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML

Also, a cleaner and more flexible approach is to use the Selectors API: 另外,一种更清洁,更灵活的方法是使用Selectors API:

http://www.w3.org/TR/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