在我的代码中,我想显示页面上第一个链接的默认内容。 下面是我的代码,在这里,当我点击某个链接时,它加载它的内容, 而不是我想显示的第一个链接内容在页面加载,在任何链接的用户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
我认为提问者故意这样做,所以他们可以使用它作为选择器字符串,但他们可以使用另一个属性,或者只是在选择时假装#。 –