gpt4 book ai didi

javascript - 是否可以使用 JavaScript 同时触发 2 个事件?

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:16 25 4
gpt4 key购买 nike

我试图同时触发 2 个事件,方法是单击右侧导航栏上的“过去位置”链接以跳转到底部页面的“过去位置”部分并路由默认图像 map (印第安纳波利斯 map ) 同时。

目前,它可以很好地跳转到页面底部,但图像 map 消失了—— map 的占位符中什么也没有显示。

HTML - 其他两个工作链接路由图像映射

<li class="j_linkHover">
<a href="#mapCO-asp" class="j_linkThumb">Aspen, CO</a>
<p class="j_accordion-panel"><span class="j_dateLocation">Central Regions<br>May 29 - June 3</span></p>
</li>

<li class="j_linkHover">
<a href="#mapOR" class="j_linkThumb">Salem, OR</a>
<p class="j_accordion-panel"><span class="j_dateLocation">Oregon School for the Deaf,<br>Washington School for the Deaf, <br> Non-Profit Community Event<br>June 3 - 6</span></p>
</li>

<li class="j_linkHover">
<a href="javascript: document.body.scrollIntoView(false);" id="location-color" class="j_linkThumb is-active">Past Locations</a> <--- jumps to the bottom, but cannot route image map at the same time
</li>

JavaScript - 这会在占位符中路由图像映射

$(document).ready(function(){

//Default Action
$(".mapActive").css({'display':'none'});
$("ul.j_linkLocation li#mapIN").addClass("active").show(); //Activate first tab
$(".mapActive#mapIN").show(); //Show first tab content

//On Click Event
$("ul.j_linkLocation li").click(function() {
$("ul.j_linkLocation li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".mapActive").css({'display':'none'}); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
$(activeTab).fadeIn('fast'); //Fade in the active content
return false;
});

});

enter image description here enter image description here

最佳答案

要重用相同的功能,请在函数中定义它:

function routeImages (elem) {
$("ul.j_linkLocation li").removeClass("active"); //Remove any "active" class
$(elem).addClass("active"); //Add "active" class to selected tab
$(".mapActive").css({'display':'none'}); //Hide all tab content
var activeTab = $(elem).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
$(activeTab).fadeIn('fast'); //Fade in the active content
}

所以现在你的旧代码变成了这样:

//On Click Event
$("ul.j_linkLocation li").click(function() {
routeImages($(this));
});

然后为列表项添加一个点击监听器:

$('#location-color').parent('li').on('click', function() {
//the first event
$('body').scrollIntoView(false);

//call the function for routing images
routeImages($('ul.j_linkLocation li:first'));
});

关于javascript - 是否可以使用 JavaScript 同时触发 2 个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50031661/

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