gpt4 book ai didi

jquery - 修改jquery函数

转载 作者:行者123 更新时间:2023-12-01 06:06:50 26 4
gpt4 key购买 nike

下面我使用jquery来切换显示div。我需要添加什么才能在页面加载时仅第一个结果处于事件状态?

$(document).ready(function(){

//Hide (Collapse) the toggle containers on load
$(".toggle_container").hide();

//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
$("h6.trigger").click(function(){
$(this).toggleClass("active").next().slideToggle("slow");
return false; //Prevent the browser jump to the link anchor
});

最佳答案

您可以使用 eq()(docs) 从原始选择中获取第一个匹配项方法或 first()(docs)方法。

$("h6.trigger").click(function(){
$(this).toggleClass("active").next().slideToggle("slow");
return false;
}).eq(0).click();
// ^------^------grab the first element, and trigger the handler
<小时/>

或者作为替代方案,您可以使用 triggerHandler()(docs)方法仅触发第一个。

$("h6.trigger").click(function(){
$(this).toggleClass("active").next().slideToggle("slow");
return false;
}).triggerHandler('click');
// -----^--------this will only trigger the click on the first element.
<小时/>

这两者都允许您利用原始 DOM 选择,从而无需重新选择。

关于jquery - 修改jquery函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4962066/

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