简体   繁体   中英

document.getElementByTag

I already used document.getElementById, but it does not work any more. I want to get the value of 0,01€ but I can not. I want to save it in my NSString *price but how. The HTML code is

<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.

If the page has jQuery loaded in it you can use jQuery('.price td:first').text() to get the price.

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 .

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 .

Also, a cleaner and more flexible approach is to use the Selectors API:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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