我已經寫了一個可以正常工作的函數,但是對於jquery而言是新手,我想知道是否有更簡潔的書寫方式。任何建議都可以幫助,只是想學習!removeclass addclass多選擇器
謝謝!
CODE:
function displayContent() {
var $link1 = $('.row.nav li a.bio');
var $link2 = $('.row.nav li a.stylist');
var $link3 = $('.row.nav li a.contact');
var $content = $('#text-content')
var $bio = $("#bio");
var $stylist = $("#stylist");
var $contact = $("#contact");
var $overlay = $('.content-overlay');
//link1
$link1.click(function (e) {
e.stopPropagation();
$link2.removeClass('active');
$link3.removeClass('active');
$link1.addClass('active');
$contact.hide();
$stylist.hide();
$bio.fadeIn(700);
$overlay.show();
});
//link2
$link2.click(function (e) {
//same code here
});
//link3
$link3.click(function (e) {
//same code here
});
//close overlay/hide content
$('html').click(function (e) {
e.stopPropagation();
$link1.removeClass('active');
$link2.removeClass('active');
$link3.removeClass('active');
$bio.fadeOut();
$stylist.fadeOut();
$overlay.fadeOut();
$contact.fadeOut();
});
}
UPDATE:
HTML標記:
<div class="main-nav pull-left">
<ul class="row nav">
<li><a href="#" class="bio">bio</a></li>
<li><a href="#" class="stylist">stylist</a></li>
<li><a href="#" class="contact">contact</a></li>
</ul>
</div>
<section id="text-content">
<div class="row">
<div id="bio" style="display: none;">
content here
</div>
<div id="stylist" style="display: none;">
content here
</div>
<div id="contact" style="display: none;">
content here
</div>
</section>
http://codereview.stackexchange.com/ – j08691
此問題屬於Stack Exchange網絡中的其他站點:http://codereview.stackexchange.com/ – Xotic750
錯誤。謝謝! – user2647510