我有以下代码
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function () {
$("#div1").hide();
$("#div2").hide();
});
</script>
</head>
<body>
<div id="div1">This is first Div</div>
<div id="div2">Welcome At 2nd Div </div>
</body>
</html>
网址是index.html。如果url像index.html#div1,我如何显示#div1;如果url像index.html#div2,我如何显示#div2
最佳答案
$(document).ready(function){
$("div[id^=div]").hide();
var loc = window.location.href;
if( loc.indexOf( '#' ) >= 0 ) {
hash = loc.substr( loc.indexOf('#') + 1 ); // output: div1, div2 etc..
$('#'+hash).show();
}
});
关于javascript - jquery div根据url显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832543/