简体   繁体   中英

JavaScript function arguments in jQuery

I am using jQuery and JavaScript. I have an onclick event that calls the JavaScript function. How do I extract the arguments of function_A and function_B in jQuery?

And is it the best way to do it?

I need to do an Ajax call after a JavaScript click. Are there any performance issues using jQuery?

My code:

<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script>
            $(document).ready(function(){
                //Here need to extract the JavaScript function arguments
                var id1=from_javascript_function_A
                var id2=from_javascript_function_B

                var link1=from_javascript_function_A
                var link2=from_javascript_function_B
                //
        </script>
    </head>
    <body>
        <a href='#' onclick=javascript:function_A('first',1)>link1</a>
        <a href='#' onclick=javascript:function_B('second',2)>link2</a>
    </body>
</html>

What about something like this?

<script>
        $(document).ready(function(){
            $("a.a").click(function(e){
                DoWork('a',1,function_a);
            });
            $("a.b").click(function(e){
                DoWork('b',2,function_b);
            });
        });

        function DoWork(s,i,f){
            f(s,i);
            alert("after click:" + s + " " + i);
        }

        function function_a(s,i){
            alert("a:" + s + " " + i);
        }
        function function_b(s,i){
            alert("b:" + s + " " + i);
        }

    </script>

</head>
<body>

    <a href="#" class="a">a</a>
    <a href="#" class="b">b</a>
</body>

If you really really want to extract the parameters from the function calls, you need to do a bunch of manual parsing, for example,

$("a").each(function(){
  var onclicktext = $(this).attr("onclick");
  if(onclicktext.indexOf("function_A") > -1){
    // Do more parsing here
  }
  if(onclicktext.indexOf("function_B") > -1){
    // Do more parsing here
  }  
});

Function arguments are meant to be visible within that function only. Typically they are variables, and their values will only have meaning at the moment the function is invoked (in your example, when the corresponding link is clicked). Within $(document).ready , their value is not relevant.

In case the arguments are not variables, they are known and you wouldn't be asking this question anyway.

If you can't edit the HTML code then all you can do is some ugly hacks:

$("a").each(function(){
  fnStr = ($(this).attr('onclick')).toString();
  re = /\"[^\"]+\"/
  alert(re.exec(fnStr));
  re = /\'[^\"]+\'/
  alert(re.exec(fnStr));
});

The above code uses regular expressions to get the parameters between quotes. Try it yourself; different browsers may have different behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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