gpt4 book ai didi

javascript - 从 javascript/jquery 中的另一个函数中使用 $(this) 单击函数调用

转载 作者:行者123 更新时间:2023-12-02 18:23:39 25 4
gpt4 key购买 nike

我有一个点击函数,如下所示

$('.page-nav li').click(function(event){

console.log("clickedTab-page-nav-first-before set ="+Session.get('clickedTab'));

Session.set('clickedTab',event.target.id);
//var sel = event.prevUntil("[class*=active]").andSelf();
var sel = $(this).prevUntil("[class*=active]").andSelf(); //find all previous li
//of li which have
//class=active
sel = sel.add(sel.eq(0).prev()); // include the that li also(Now all li elements).
sel.removeClass('active'); //Remove the active.
//sel = event.nextUntil("[class*=active]").andSelf(); //Also for top to bottom
//(Viceversa)
sel = $(this).nextUntil("[class*=active]").andSelf();
sel = sel.add(sel.eq(-1).next());
sel.removeClass('active');
//event.addClass('active');
$(this).addClass('active'); //Now add class active for the clicked li
var rightcontent="";

console.log("clickedTab-page-nav-second-after set = "+Session.get('clickedTab'));

switch($(this).attr('id')){
case 'rfq':
.......
.....
}
});

接下来是我想从另一个地方调用这个点击函数

$(document).ready(function() {
console.log("clickedTab-page load = "+Session.get('clickedTab'));
if(Session.get('clickedTab')!=null||Session.get('clickedTab')!= undefined){
alert("Got It");
//$('.page-nav li').click(event);
$('.page-nav li').click(); //this is not working
}
});

现在的问题是 if 条件下的页面点击功能不起作用。不过我收到了警报。非常感谢任何帮助。提前致谢...

最佳答案

您并没有真正在函数中使用 event 参数,并且您声明希望在事件链之外调用它,以便可以将其更改为常规函数

var setupClicktab = function(id){

console.log("clickedTab-page-nav-first-before set ="+Session.get('clickedTab'));

Session.set('clickedTab',id);
...
}

你会这样使用它:

$('.page-nav li').click(function(event){return setupClicktab(event.target.id);});

文档准备就绪

setupClicktab.Call($('.page-nav li'),Session.get('clickedTab'));

后者在选择的上下文中调用它的类(即函数内的 this 将引用选择(1)。它还将存储在 session 变量中的值作为 id 传递。

附注。您的测试

 if(Session.get('clickedTab')!=null||Session.get('clickedTab')!= undefined)

可能只是

if(Session.get('clickedTab'))

除非您可以在该变量中存储空字符串、零或 bool 值 false。但看看它是如何使用的,这是不可能的,因为它们都是 id 属性的无效值

(1)这与点击事件中引用 DOM 元素略有不同)

关于javascript - 从 javascript/jquery 中的另一个函数中使用 $(this) 单击函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18589261/

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