我已经设置了一个页面,通过AJAX加载数据,利用jQuery .load函数.
通过单击选项卡栏上的链接加载每个新文件后,我使用jQuery将选定选项卡的颜色设置为黄色.我尝试使用.toggleClass函数将li元素的类设置为活动状态,因此它是黄色的,但没有骰子,所以我每次都使用重置CSS.
如何消除冗余代码或彻底检查脚本?
无论如何,这是jQuery脚本.欢迎任何帮助!
$(document).ready(function () {
$('a#catalog').click(function() {
$("#nav ul li a").css("color","white");
$(this).css("color","yellow");
$("#content").load("files/catalog.html");
});
$('a#request').click(function() {
$("#nav ul li a").css("color","white");
$(this).css("color","yellow");
$("#content").load("files/request.html");
});
$('a#publisher').click(function() {
$("#nav ul li a").css("color","white");
$(this).css("color","yellow");
$("#content").load("files/publisher.html");
});
$('a#incoming').click(function() {
$("#nav ul li a").css("color","white");
$(this).css("color","yellow");
$("#content").load("files/incoming.html");
});
$('a#finished').click(function() {
$("#nav ul li a").css("color","white");
$(this).css("color","yellow");
$("#content").load("files/finished.html");
});
$('a#shipments').click(function() {
$("#nav ul li a").css("color","white");
$(this).css("color","yellow");
$("#content").load("files/shipments.html");
});
});
和导航栏:
<div class="bar" id="nav">
<ul class="left">
<li><a href="#" id="request">Request Search</a></li>
<li><a href="#" id="catalog">Catalog Search</a></li>
<li><a href="#" id="publisher">Request from Publisher</a></li>
<li><a href="#" id="incoming">Catalog Incoming Files</a></li>
<li><a href="#" id="finished">Send Finished Files</a></li>
<li><a href="#" id="shipments">Shipments</a></li>
</ul>
</div>
最后,但并非最不重要的,CSS:
.bar { margin: 5px 0; position: relative; height: 20px; background-color: #515e6c; }
.bar a { color: #fff; }
.bar ul li a:hover { color: yellow; }
/* .bar ul li.active { color: yellow; } */
ul { margin: .3em 0; }
ul li { display: inline; padding: 0 5px; border-right: 1px solid #fff; }
ul li:last-child { border-right: none; }
提前致谢!
解决方法:
这应该这样做:
$(document).ready(function () {
var $links = $('#nav ul li a');
$links.click(function() {
$links.css("color","white");
$(this).css("color","yellow");
$("#content").load("files/" + $(this).attr('id') + ".html");
});
});
就您选择的规则而言,将其更改为:
.bar ul li a.active { color: yellow; }
然后你可以将click函数的前两行更改为:
$links.removeClass('active');
$(this).addClass('active');
该风格最初应用于< li>本身而不是< a>.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。