2014-10-07 76 views
-1

我想使用jQuery html()方法檢索整個頁腳標記內容,並且最終輸出應該位於一個變量中。jQuery鏈方法不起作用

我嘗試使用jQuery鏈方法,但它不工作。下面的代碼出了什麼問題。在使用jQuery html()方法檢索後,鏈接不應該是可點擊的。

<!DOCTYPE html> 
<html> 
<head> 

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
<script> 
$(document).ready(function(){ 
var footercontent = $('footer').html().removeAttr('href'); 
alert(footercontent); 
}); 
</script> 
</head> 
<body> 
<footer> 
<a href="#">site Map</a> | <a href="#">Privacy stat</a> | <a href="#">Tutorials</a> 
<p>Anchor tag link should be removed without content alignment changed.</p> 
<p>This is another paragraph.</p> 
<p>This is another paragraph.</p> 
<p>Anchor tag link should be removed without content alignment changed.</p> 
</footer> 
</body> 
</html> 
+1

的''

元素不包含'href'屬性,因此沒有刪除。您必須定位實際包含屬性的特定元素。我建議你閱讀[jQuery文檔](http://api.jquery.com/removeattr/),非常關注定義和例子。 – Sparky 2014-10-07 02:47:35

+0

你試圖達到的目標也很不明確。你爲什麼要從所有的錨標籤中刪除'href'屬性? – Sparky 2014-10-07 02:51:26

+0

'.html()',不帶參數調用,返回匹配元素的html。在這種情況下,您將'.removeAttr()'作爲未定義的字符串的方法調用,因此鏈斷裂。 - JAAulde 3 – JAAulde 2014-10-07 02:51:47

回答

0

的代碼應閱讀:

$('footer a').removeAttr('href'); 

您正在試圖刪除頁腳元素的href屬性,但你需要從頁腳中的任何一個標籤移除它。

1

試試這個

$(document).ready(function(){ 
     $('footer').find('a').removeAttr('href'); 
     var footercontent = $('footer').html(); 
     alert(footercontent); 
}); 
+0

假設OP確實想要更新他/她的HTML代碼,這在發佈的代碼中是不清楚的。 – 2014-10-07 02:54:36

+0

他仍然可以禁用所有的鏈接,而不使用此 '$( '尾')更新他/她的HTML代碼中找到( 'A')點擊(函數(E){ e.preventDefault(); });' whith這個沒有html代碼將被更新 – Ahmad 2014-10-07 02:58:37

+0

謝謝艾哈邁德。它的工作很好 – SPN 2014-10-07 03:01:01