jQuery equivalent to [function].parameters from javascript
I've had this javascript function that I've used many many times over ...
function showHideObjects()
{
var args;
args = showHideObjects.arguments;
for(var i=0;i<args.length;i++)
{
if(document.getElementById(args[i]))
{
if(args[i+1] == 'flip')
{
开发者_如何学Python if(document.getElementById(args[i]).style.display == '')
{ document.getElementById(args[i]).style.display = 'none';}
else
{ document.getElementById(args[i]).style.display = '';}
}
else
{
document.getElementById(args[i]).style.display = args[i+1];
}
}
i++;
}
}
Now I'm working with ASP.NET and need that same function but in jQuery but I can't find any information about dynamic parameters in jQuery. Is there a way to do this in jQuery?
To provide a little more background ... you could call the above code with a line like ... showHideObjects('div1','none')
and it'd hide div1. Or you could call ... showHideObjects('div1','none','div2','','div3','flip')
and it'd hide div1, show div2 and switch div3 from either hidden or shown.
jQuery is just JavaScript. Your code will work fine with jQuery.
It is purely a javascript feature. Jquery do not have any special feature for this.
As some have already said, jQuery is Javascript and your code will work just fine. BUT you can do the same thing with jQuery that you are now doing with your function. You are getting elements which you change to have either display none or nothing.
jQuery has Selectors, which you can use to select the elements from the DOM to which you want to do something to. You can forexample use selectors to select the element you want and then just cast .toggle(). It does the exactly same thing as your function but with just 1 line of code.
EDIT: Added an example:
If you have a div, which has a class hideNseek, you can use this to toggle it to show or hide:
$(".hideNseek").toggle();
$(".hideNseek") is selectors which selects all classes that have that name.
Like others have said, your function will work just fine. You don't NEED jQuery to do what you are doing. If you wanted to use jQuery you could do something like:
function showHideObjects() {
var args = arguments,
l=args.length;
for ( var i=0; i<l; i++ ) {
var elem = $( "#"+args[i] ),
type = args[i+1];
if ( elem.length ) {
if ( type == "flip" ) {
elem.toggle();
} else {
elem.css("display", type);
}
}
i++
}
}
You can't call different actions on a group in JQuery. However, you can perform the same action (hide, show, toggle) on a group of elements by using the appropriate selector to get the group. Here's a quick example that has buttons to toggle all the div elements and all the span elements on the page with the JQuery toggle function, along with a fiddle for testing:
HTML:
<div>div 1</div>
<span>span 1</span>
<div>div 2</div>
<span>span 1</span>
<input id="toggleSpans" type="button" value="toggle spans"/>
<input id="toggleDivs" type="button" value="toggle divs"/>
JavaScript:
$('#toggleDivs').click(function(){
$('div').toggle();
});
$('#toggleSpans').click(function(){
$('span').toggle();
});
If that is not good enough, you can easily group and call these methods in a custom function as you do in your old JavaScript function, or just keep your old method.
UPDATE:
Check out the different selectors you can use. In your specific case, you'd need to use ID Selector along with Multiple Selector, but you could make your function much more powerful if you allowed passing in any JQuery selector rather than just the ids. For example:
showHideObjects('#div1, <otherSelectorsToHide>','none',
'#div2, <otherSelectorsToShow>','',
'#div3, <otherSelectorsToToggle>','flip');
精彩评论