Jquery对不同事件的功能相同

时间:2012-06-11 05:24:32

标签: javascript jquery

我的代码就像这样

    $(document).ready(function(){     
        nodeClick(elm = '');   
    });

function nodeClick()
{ 
    if(elm != ''){
        var obj = '';
    }else{
        obj = $('.hitarea')
    }

    obj.live('click',function(){   
    $(this).addClass('something');
    $(this).attr('something',1);
    //lots of this reference are here


    });

}

如果elm变量为空,我需要自动调用click函数,即不用click.ow可以触发该函数而无需点击

2 个答案:

答案 0 :(得分:0)

函数nodeclick in不带任何参数并传递它。在其中添加参数,然后在这样的调用中传递它。

 $(document).ready(function(){     
     elm = '';
        nodeClick();   
    });

function nodeClick(elm)
{ 
    if(elm != ''){
        var obj = '';
    }else{
        obj = $('.hitarea')
    }

    obj.live('click',function(){   
    $(this).addClass('something');
    $(this).attr('something',1);
    //lots of this reference are there are

        /****
          if elm variable is empty i  need to call these function automatically ie        with out click.how can i trigger that function with out click
       ******/
    });

   obj.click(); //You can call click event handler like this


}

答案 1 :(得分:0)

我对措辞很困惑,但要触发click事件,请使用.trigger()

obj.trigger('click');