這是一個超級簡單的問題,我似乎無法找到一個好的答案了。jQuery GET html作爲遍歷jQuery對象
$.get('/myurl.html', function(response){
console.log(response); //works!
console.log($(response).find('#element').text()); //null :(
}, 'html');
我只是想遍歷我的html響應。到目前爲止,我能想到的唯一辦法就是將正則表達式內置到body標籤中,並將其用作字符串來創建我的遍歷jQuery對象。但那似乎很愚蠢。任何人都在意指出正確的方式來做到這一點?
也許是我的html?
<html>
<head>
<title>Center</title>
</head>
<body>
<!-- tons-o-stuff -->
</body>
</html>
這也工作正常,但並不適合我的需求:
$( '#myelem')負載( '/ myurl.html #element');
如果您在響應html中尋找具有值爲「element」的「id」屬性的某個元素,那麼它對我來說看起來是正確的。我不知道爲什麼這不起作用。 – jessegavin 2012-04-05 16:44:07
你提取的html是怎麼樣的? – 2012-04-05 16:44:15
這應該回答你的問題: http://stackoverflow.com/questions/9540218/a-javascript-parser-for-dom/9540313#9540313 – 2012-04-05 16:46:07