如何从点击到实时功能传递params?

时间:2012-09-25 09:18:14

标签: javascript jquery

我有两个功能点击并直播。我想将一个参数从点击传递给live.I尝试了类似下面的东西,但是它没有用。

jQuery(document).ready(function(){
  var test = 'test' ;                               
  jQuery('.item a').click(test);//pass an argument from here
});

jQuery('.item a').live('click',function(e,test) {
  alert(test);//access argument here
});

这可能吗?

更新

function init() {   
    //When you click on a link
    jQuery('.item a').live('click',function(e,test) {
        alert(test);
    });
}
jQuery(document).ready(init);
jQuery(document).ready(function(){
    var test= 'test';
    jQuery('.item a').trigger('click', test);   
});

我期待一个提醒。

EDIT2:

jQuery(document).ready(function(){
    (function($){
        $('.item').each(function() {
            $(this)[0].oncontextmenu = function() { return false }
          });
        $.fn.ctrl = function(key, callback) {
            if(typeof key != 'object') key = [key];
            callback = callback || function(){ return false; }
            return $(this).keydown(function(e) {
                var ret = true;
                $.each(key,function(i,k){
                    if(e.keyCode == k.toUpperCase().charCodeAt(0) && e.ctrlKey) {
                        ret = callback(e);
                    }
                });
                return ret;
            });
        };

        $.fn.disableSelection = function() {
            $(window).ctrl(['a','s','c']);
            return this.each(function() {           
                $(this).attr('unselectable', 'on')
                       .css({'-moz-user-select':'none',
                            '-o-user-select':'none',
                            '-khtml-user-select':'none',
                            '-webkit-user-select':'none',
                            '-ms-user-select':'none',
                            'user-select':'none'})
                       .each(function() {
                            $(this).attr('unselectable','on')
                            .bind('selectstart',function(){ return false; });
                       });
            });
        };
        $('.item').disableSelection();
    })(jQuery);             

});

感谢。

3 个答案:

答案 0 :(得分:6)

您可以改为使用trigger(),它允许您传递参数;

jQuery(document).ready(function(){
  var test = 'test' ;                               
  jQuery('.item a').trigger('click', test); //pass an argument from here
});

jQuery('.item a').live('click',function(e,test) {
  alert(test);//access argument here
});

答案 1 :(得分:1)

或者您可以使用隐藏字段来存储需要传递的参数,然后在点击功能中读取它

jQuery(document).ready(function(){
  var test = 'test' ;
  jQuery('#hiddenID').val(test);
  jQuery('.item a').click(test);//pass an argument from here
});

jQuery('.item a').live('click',function(e,test) {
  var test = jQuery('#hiddenID').val();
  alert(test);//access argument here
});

答案 2 :(得分:1)

你也可以尝试这个

jQuery(document).ready(function(){
    var test = 'test' ;                               
    $('.item a').trigger({type:'click', myParam:test}); // pass the event object with param
});

$('.item a').live('click',function(e) {
    alert(e.myParam); //access param here
});

另请注意,{@ 1}}已弃用,而是使用on

<强>更新

live
相关问题