gpt4 book ai didi

javascript - 编码不当的 JQuery : naming dependancies

转载 作者:行者123 更新时间:2023-12-02 19:22:08 26 4
gpt4 key购买 nike

我的页面上有几个带有此标记的选项卡

<div id="holiday-details-nav">
<ul>
<li><a class="holidaydetails-description" title="Overview" href="#tab-holidaydetails-overview">Overview</a></li>
<li><a class="holidaydetails-included" title="Rooms" href="#tab-holidaydetails-rooms">Rooms</a></li>
<li><a class="holidaydetails-itinerary" title="Rates" href="#tab-holidaydetails-rates">Rates</a></li>
<li><a class="holidaydetails-accommodation" title="Information" href="#tab-holidaydetails-information">Information</a></li>
<li><a class="holidaydetails-reviews" title="Reviews" href="#tab-holidaydetails-reviews">Reviews</a></li>
</ul>
</div>

这些选项卡中内容的隐藏和显示由一些由此开始的 JQuery 代码控制

$(document).ready(function () {
// Hide all tabs apart from the overview
$('#holiday-details-tabs div:#tab-holidaydetails-rooms').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-rates').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-information').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-reviews').hide();
...

问题是,如果我添加、删除或重命名选项卡(就像我刚刚所做的那样),那么我必须更改所有这些代码。我想要的是添加、重命名或删除任意数量的选项卡,但不必修改此 JQuery 代码。

我并不是真的在寻找有人为我编写解决方案,而是想开始讨论可用于避免这种命名依赖的工具、技术等。

编辑

当单击选项卡时,我也有一点丑陋。

$('#holiday-details-nav ul li a').click(function () {
// Remove active class from all links
$('#holiday-details-nav ul li').removeClass('active');
//Set clicked link class to active
$(this).parent().addClass('active');
// Set variable currentTab to value of href attribute of clicked link
var currentTab = $(this).attr('href');
// Hide all tabs
$('#holidaydetails-description-imagecontainer').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-overview').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-rooms').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-rates').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-information').hide();
$('#holiday-details-tabs div:#tab-holidaydetails-reviews').hide();

$('#holiday-details-bottom').show();
$('#holiday-details-left-booknow').show();

// Show div with id equal to variable currentTab
$(currentTab).show();

$('#holidaydetails-description-imagecontainer').show();

return false;
});

谢谢

萨钦

最佳答案

您可以为每个li 除了概述分配一个通用 CSS类,例如tab,然后使用 jQuery 类选择器来隐藏它们。例如:

<div id="holiday-details-nav">
<ul>
<li><a class="holidaydetails-description" title="Overview" href="#tab-holidaydetails-overview">Overview</a></li>
<li><a class="tab holidaydetails-included" title="Rooms" href="#tab-holidaydetails-rooms">Rooms</a></li>
<li><a class="tab holidaydetails-itinerary" title="Rates" href="#tab-holidaydetails-rates">Rates</a></li>
<li><a class="tab holidaydetails-accommodation" title="Information" href="#tab-holidaydetails-information">Information</a></li>
<li><a class="tab holidaydetails-reviews" title="Reviews" href="#tab-holidaydetails-reviews">Reviews</a></li>
</ul>
</div>

然后,隐藏除概述之外的所有选项卡:

$("holiday-details-nav .tab").hide();

或者相反,即将特定的类添加到概述中,并隐藏所有其他选项卡:

<div id="holiday-details-nav">
<ul>
<li><a class="overview holidaydetails-description" title="Overview" href="#tab-holidaydetails-overview">Overview</a></li>
<li><a class="holidaydetails-included" title="Rooms" href="#tab-holidaydetails-rooms">Rooms</a></li>
<li><a class="holidaydetails-itinerary" title="Rates" href="#tab-holidaydetails-rates">Rates</a></li>
<li><a class="holidaydetails-accommodation" title="Information" href="#tab-holidaydetails-information">Information</a></li>
<li><a class="holidaydetails-reviews" title="Reviews" href="#tab-holidaydetails-reviews">Reviews</a></li>
</ul>
</div>

然后,要隐藏,请选择所有选项卡,并使用 .not() 排除概览:

$("#holiday-details-nav a").not(".overview").hide();

关于javascript - 编码不当的 JQuery : naming dependancies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12428590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com