What exactly are parameters in javascript (or any programming language for that matter)
I have this piece of code that I've written and read that in order for it to work correctly I must use parameters so I did and it works perfectly, however I cant figure out for the life of me what parameters are and how they work. I read through a ton of articles all over the web but I just couldn't figure out how parameters work. How does one parameter know to grab instructions from another. The whole idea is just really frustrating. Also this is kind of a side question. Can I getEl开发者_如何学编程ementBy Class instead of Id or is there anything similar to get getElementById()
for classes? Thanks so much in advance.
Below is the code that is in the script.js file:
function setValue(field)
{
if(''!=field.defaultValue)
{
if(field.value==field.defaultValue)
{
field.value='';
}
else if(''==field.value)
{
field.value=field.defaultValue;
}
}
}
and I called this script to run with the code below:
<textarea id="info"
class="textArea"
name="comment"
cols="40" rows="10"
onfocus="setValue(this)"
onblur="setValue(this)">
Whats Your Name
</textarea>
Parameters
If by "parameter" you mean "argument", it's not at all clear what you mean by "How does one parameter know to grab instructions from another." Arguments/parameters don't grab "instructions" from each other.
Since it's not at all clear what you're actually asking here, I won't go into any kind of detail, but I will warn that function arguments actually work a bit differently in JavaScript than in many other languages like C, C#, or Java.
The traditional model is a special memory area called a "stack": The caller pushes arguments onto the stack, and the callee (the function being called) pulls them off the stack.
JavaScript doesn't use the stack model, though. Instead, when a function is called, an object called an execution context is allocated, and along with it something called a variable object, and the arguments (and a few other things) end up being properties on the variable object. (This happens invisibly behind the scenes, you don't actually get direct references to either object, but the fact of them is clear from edge case behaviors.)
Getting Elements by Class Name
There's getElementsByClassName
which is widely-supported except by IE. But if you search for "getElementsByClassName
IE" you'll find a variety of implementations for IE that you can drop into your page.
When I hear the word "parameter", I usually think of references that are passed into methods for execution. Is that what you mean? Whether it's a function or an object method, that's usually the name that we give to the object references that are passed in. Is that what you have in mind?
Yes, it's possible to query for a DOM element using div names, as long as you've written your page to do so. jQuery is the library that most people are using to manipulate the DOM in an HTML page. It has lots of methods for querying for different DOM elements. Perhaps that is what you want.
Well, I think many people call them parameters while many others call them arguments, but they are both the same: They are what you pass to a function. What that does with the parameters/arguments is completely dependent on the function. You could pass a DOM element, a string, an object, you name it.. but the function ultimately decides what to do with it.
Your side-question about getElementByClass, there is getElementsbyClassName, but its not cross-browser compatible, meaning it only works in certain browsers. There are libraries that handle all of the cross-browser madness for you though, such as Sizzle.
精彩评论