简体   繁体   English

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

[英]click function call with $(this) from another function in javascript/jquery

I have a click function which is given below 我有一个点击功能,如下所示

$('.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':
               .......
               .....
        }
});

Then next is I want to call this click function from another place 接下来我想从另一个地方调用这个点击功能

$(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
     }
});

Now the problem is page click function in if condition is not working. 现在问题是如果条件不起作用的页面点击功能。 However I got the alert. 但是我得到了警报。 Any help is highly appreciated. 任何帮助都非常感谢。 Thanks in advance... 提前致谢...

You need to put $('.page-nav li').click(function(event){ inside document.ready and before your $('.page-nav li').click(); . Because if you call .click when the DOM is not ready, there are chances that there is no event handler attached 你需要把$('.page-nav li').click(function(event){里面document.ready和你之前 $('.page-nav li').click();因为如果你打电话.click在DOM未准备好时.click ,可能没有附加事件处理程序

If you don't put $('.page-nav li').click(function(event){ inside document.ready OR you're dealing with dynamically created elements. You need delegated event $(document).on("click",".page-nav li",function(event){ 如果你没有放$('.page-nav li').click(function(event){ inside document.ready OR你正在处理动态创建的元素。你需要委托事件$(document).on("click",".page-nav li",function(event){

From $.on $ .on

you are not really using the event parameter in your function and you state you wish to call it outside of an event chain so you could change it to be a regular function 你实际上并没有在你的函数中使用event参数,并且你声明你希望在事件链之外调用它,所以你可以将它改为常规函数

var setupClicktab = function(id){

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

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

the you'd use it like: 你会像以下一样使用它:

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

and in document ready 并准备好文件

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

The latter call class it in the context of the selection (that is this inside the function will refer to the selection(1). It also passes the value stored in the session variable in as the id. 后者在选择的上下文中调用它(即函数内部将引用选择(1)。它还将存储在会话变量中的值作为id传递。

a side note. 旁注。 Your test 你的考试

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

could simply be 可能只是

if(Session.get('clickedTab'))

Unless you might store either an empty string, zero or the boolean value false in that variable. 除非你可以在该变量中存储空字符串,零或布尔值false。 But seeing how it's used that's unlikely since they are all invalid values for the id attribute 但是看看它是如何使用的,因为它们都是id属性的无效值

(1)This is slightly different than in the click event where it refers to the DOM element) (1)这与它引用DOM元素的click事件略有不同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM