簡體   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