pee*_*lba 3 javascript css ajax jquery
我已经设置了一个页面,通过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");
});
});
Run Code Online (Sandbox Code Playgroud)
和导航栏:
<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>
Run Code Online (Sandbox Code Playgroud)
最后,但并非最不重要的,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; }
Run Code Online (Sandbox Code Playgroud)
提前致谢!
Pao*_*ino 13
这应该这样做:
$(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");
});
});
Run Code Online (Sandbox Code Playgroud)
就您选择的规则而言,将其更改为:
.bar ul li a.active { color: yellow; }
Run Code Online (Sandbox Code Playgroud)
然后你可以将click函数的前两行更改为:
$links.removeClass('active');
$(this).addClass('active');
Run Code Online (Sandbox Code Playgroud)
这种风格最初是应用于<li>自身而不是<a>.