我写了一个你要做什么的例子:
我认为您的 html 代码是这样的:
<html>
<head>
<title></title>
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/resize.js"></script>
</head>
<body>
<div id="navigation">
<a href="#" id="link1">link1</a> <span id="2">|</span> <a href="#" id="link2">link2</a> <span id="3">|</span> <a href="#" id="link3">link3</a> <span id="4">|</span> <a href="#" id="link4">link4</a> <span id="5">|</span> <a href="#" id="link5">link5</a>
</div>
</body>
</html>
如您所见,我添加了一个 javascript 标签<script type="text/javascript" src="js/resize.js"></script> in the head of the page
,该文件如下所示:
// resize.js
$(document).ready(function(){
// add a (more) link with javascript to ensure that it exist only if javascript is enabled
$("#navigation").append('<a href="#" id="more">more</a>'); // #navigation is a div container of our links
// in the load of document , you check the width of the browser and apply hide or show links switch what do you need
var width_of_window = $(window).width();
if (width_of_window <= 560) {
$("#link5, #link4, #5, #4").hide();
}
if(width_of_window > 560 && width_of_window <= 600){
$("#link4").show(); $("#link5").hide();
}
if(width_of_window >= 640){
$("#more").hide();
}
// here resize function is handled when you resize the navigator
$(window).resize(function() {
width_of_window = $(window).width(); // get the width of the window each time you resize it
//apply what do you need
if (width_of_window <= 560) {
$("#link5, #link4, #5, #4").hide(); // #link5 and link4 are the id of links and #5, #4 are separator '|' between links (i added | separator between span, see html code)
if( $("#more").is(':hidden') ){
$("#more").show();
}
}
if(width_of_window > 560 && width_of_window <= 600){
$("#link4").show(); $("#link5").hide();
if( $("#more").is(':hidden') ){
$("#more").show();
}
}
if(width_of_window >= 640){
$("#more").hide();
$("#link4, #link5").show();
}
});
});
有关调整大小功能的更多详细信息,您可以在此处查看文档.resize() | jQuery
希望对你有帮助,如果你对代码有任何疑问,请写评论^^