gpt4 book ai didi

javascript - 在 javascript 中制作一个带有事件菜单的菜单吗?

转载 作者:行者123 更新时间:2023-12-03 11:35:14 24 4
gpt4 key购买 nike

我有这个代码<强> http://jsfiddle.net/9nd4j/1722/

$('ul.navi').each(function () {
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');

// If the location.hash matches one of the links, use that as the active tab.
// If no match is found, use the first link as the initial active tab.
$active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
$active.addClass('active');

$content = $($active[0].hash);

// Hide the remaining content
$links.not($active).each(function () {
$(this.hash).hide();
});

// Bind the click event handler
$(this).on('click', 'a', function(e){
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();

// Update the variables with the new link and content
$active = $(this);
$content = $(this.hash);

// Make the tab active.
$active.addClass('active');
$content.show();

// Prevent the anchor's default click action
e.preventDefault();
});
});

我想一次向“Licenciatura”和“MEstrado”显示文本,而不是同时向两者显示文本。另一个问题是事件类,只是打算一次包含一个事件项目,而不是“Licenciatura”和“Mestrado”之一

最佳答案

你可以这样做:

$('ul.navi').on('click', 'a', function (e) {
//Change content displayed
$($("ul.navi a.active")[0].hash).hide();
$(this.hash).show();

//Change active item
$("ul.navi a.active").removeClass("active");
$(this).addClass("active");

e.preventDefault();
});

//Hide all content divs except first one
$("ul.navi a").each(function(index){
if(index != 0)
$(this.hash).hide();
else
$(this).addClass("active");
});

在这里查看:JSFiddle

关于javascript - 在 javascript 中制作一个带有事件菜单的菜单吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26547130/

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