最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing parameters by reference to click event in Jquery - Stack Overflow

programmeradmin2浏览0评论

I have one function :

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

and in the function I am retreving window.location.hash value :

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 ? 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.

I have one function :

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

and in the function I am retreving window.location.hash value :

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 ? 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.

Share Improve this question asked Mar 24, 2011 at 3:02 TopCoderTopCoder 4,29619 gold badges56 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can't really pass a value by reference in JavaScript but you can pass something that is already a reference. 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:

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.

发布评论

评论列表(0)

  1. 暂无评论