在我的代碼中,我想顯示頁面上第一個鏈接的默認內容。 下面是我的代碼,在這裏,當我點擊某個鏈接時,它加載它的內容, 而不是我想顯示的第一個鏈接內容在頁面加載,在任何鏈接的用戶cliks後,內容必須得到改變顯示加載時的默認頁面
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="nav">
<a href="" id="#content1">Show content 1</a>
<a href="" id="#content2">Show content 2</a>
<a href="" id="#content3">Show content 3</a>
</div>
<div id="contents" style="width: 200px; height: 40px; border: dotted; margin-top: 20px;">
<div id="content1" class="toggle" style="display:none">show the stuff1</div>
<div id="content2" class="toggle" style="display:none">show the stuff2</div>
<div id="content3" class="toggle" style="display:none">show the stuff3</div>
</div>
<script>
$("#nav a").click(function(e){
e.preventDefault();
$(".toggle").hide();
var toShow = $(this).attr('id');
$(toShow).show();
});
</script>
</body>
</html>
下面的jsfiddle鏈接
什麼這裏的回答者想說的是,你不小心添加在ID前加一個'#',這是違背標準的,你可以在這裏閱讀更多的內容:http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id -attr ibute-in-html – h2ooooooo
我認爲提問者故意這樣做,所以他們可以使用它作爲選擇器字符串,但他們可以使用另一個屬性,或者只是在選擇時假裝#。 –