简体   繁体   English

通过引用Jquery中的click事件传递参数

[英]Passing parameters by reference to click event in Jquery

I have one function : 我有一个功能:

jQuery('#'+'linkhere').click(getHashPageClick);

and in the function I am retreving window.location.hash value : 并在函数我正在retreving window.location.hash值:

var getHashPageClick = function(evt) {
    var hashParam =  window.location.hash.substring(1);
}

But now I want to send a parameter to getHashPageClick(), say value , append hashParam value to it and access it in my calling function ? 但是现在我想向getHashPageClick()发送一个参数,比如说值,将hashParam值附加到它并在我的调用函数中访问它? So something like this, maybe? 也许这样的事情呢? :

jQuery('#'+'linkhere').click(getHashPageClick(value);
 alert("value");

and in the called function modify value of value : 并在被调用函数中修改值的值:

    var getHashPageClick = function(evt,value) {
        var hashParam =  window.location.hash.substring(1);
        valuevalue+hashParam;
return value;

}

Would that be acceptable, or is there a different way to send a parameter by reference so that it is accessible in the calling function and modified value should be reflected ? 这是可以接受的,还是有不同的方式通过引用发送参数,以便在调用函数中可以访问它,并且应该反映修改后的值? Because this way doesn't seem right to me. 因为这种方式对我来说似乎不对。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

You can't really pass a value by reference in JavaScript but you can pass something that is already a reference. 您无法在JavaScript中通过引用传递值,但您可以传递已经是引用的内容。 For example, you could use an object literal to simulate a pointer: 例如,您可以使用对象文字来模拟指针:

function getHashPageClick(p) {
    p.value = p.value + window.location.hash.substring(1);
}

var p = { value: 'some value' };
getHashPageClick(p);

Here's how I would go about setting up the functionality you're looking for: 以下是我将如何设置您正在寻找的功能:

jQuery(document).ready(function(){
     var value = 'some value';
     jQuery('#linkhere').click(function(){
          value = getHashPageClick(value);
          alert('the value is now: '+value);
     });
});

var getHashPageClick(value) {
        var hashParam =  window.location.hash.substring(1);
        value = value+hashParam;
        return value;
}

Or, if you don't want to bump heads with just the one 'value' variable, create two variables to work in tandem: 或者,如果您不想只使用一个'value'变量来创建头部,请创建两个变量以协同工作:

jQuery(document).ready(function(){
     var value = 'some value';
     jQuery('#linkhere').click(function(){
          hashValue = getHashPageClick(value);
          alert('the hash value is now: '+hashValue);
     });
});

var getHashPageClick(value) {
        var hashParam =  window.location.hash.substring(1);
        var hashValue = value+hashParam;
        return hashValue;
}

Hope this helps. 希望这可以帮助。

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

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