如何獲取html代碼中的數字。使用jquery/javascript從一行html代碼中獲取數字
例如:
<a href="#">Comments</a>(10)
如何從上面的代碼線得到10。
這包括移除鏈接HTML,評論的話,再加上週圍
如何才能做到這一點使用jQuery/JavaScript的數字「10」的括號?
如何獲取html代碼中的數字。使用jquery/javascript從一行html代碼中獲取數字
例如:
<a href="#">Comments</a>(10)
如何從上面的代碼線得到10。
這包括移除鏈接HTML,評論的話,再加上週圍
如何才能做到這一點使用jQuery/JavaScript的數字「10」的括號?
給定的HTML
<a id="#element" href="#">Comments</a>(10)
的jQuery:
var value = $("#element").get(0).nextSibling.nodeValue; //"(10)"
value = parseFloat(value.substr(1)); //10
如果你有一個字符串('<a href="#">Comments</a>(10)'
),然後
var value = $('<div>', {html: '<a href="#">Comments</a>(10)'}).contents().get(1).nodeValue; //"(10)"
value = parseFloat(value.substr(1)); //10
你可以嘗試這樣的事情: http://pastebin.com/PV9f0PQH
我在pastebin中粘貼示例,因爲在粘貼部分html標籤時出現問題。
你能否提供包含html元素?(dom結構) – Ando 2012-07-20 06:32:32
上面這行是從html頁面中提取的,並存儲在一個javascript變量中。 – sm21guy 2012-07-20 06:34:17