0

Is there any way to pass the arguments to jquery/javascript like below

function showContentDiv(args ......){
 //do some thing with looping args
}

and usage like

showContent("","","","");  //should change the parma count each time 

Reason behind avoid preparing a array in jquery and passing is I have links like below

<a onclick="javaScript:showContent("val1","val2","val3")">

<a onclick="javaScript:showContent("val1","val2")">
4

2 回答 2

4

JavaScript functions automatically have an arguments object - an array-like object that includes elements for every parameter passed to the function.

The following example simply loops through any arguments and logs them to the console.

function showContentDiv(){
   for (var i = 0; i < arguments.length; i++) {
       console.log(arguments[i]);
   }
}

As an aside, don't include javaScript: at the beginning of your event attributes. This isn't required by any browser. onclick="showContent()" is fine. (Where by "fine" I mean "not as good as binding the events in a script block".)

于 2013-06-22T13:26:38.003 回答
1

try this

<a onclick="javaScript:showContent("val1,val2,val3")">

function showContentDiv(args){
var argsList=args.split(',');
for(i=0;i<argsList.length;i++)
{
argsList[i]
}
 //do some thing with looping args
}
于 2013-06-22T13:32:02.500 回答