1
Do add in a
添加一个
$(document).ready(function(){
});
This waits to execute the code inside until everything has finished loading. That way it shouldn't return undefined.
这等待执行内部代码,直到所有内容都加载完毕。这样它就不应该返回undefined。
ALSO
I couldn't help but noticing that you seem to be trying to give multiple items the same ID.
我忍不住注意到你似乎试图给多个项目提供相同的ID。
Don't use IDs for multiple elements. That's not how they are designed to be used, nor do they work that way.If you give multiple elements the same ID and then try and style them with CSS, it'll only style the first one. Use a class. If you use
不要将ID用于多个元素。这不是它们的设计使用方式,它们也不是那样工作的。如果你给多个元素提供相同的ID,然后尝试用CSS设置它们,那么它只会设计第一个。使用课程。如果你使用
document.getElementById();
to try and grab multiple elements with the same ID, then the script will ONLY grab the FIRST element with that ID, because, given that it is an ID, it expects only one element. If you want to work with multiple elements, use a class, and then use
尝试使用相同的ID来获取多个元素,然后脚本将仅使用该ID获取FIRST元素,因为,鉴于它是一个ID,它只需要一个元素。如果要使用多个元素,请使用类,然后使用
document.getElementsByClassName();
this will grab ALL elements with that class. So for example, say you have four span elements with the class "foo". To grab all these and change the text, do this:
这将获取该类的所有元素。例如,假设您有四个带有“foo”类的span元素。要获取所有这些并更改文本,请执行以下操作:
elements=document.getElementsByClassName("foo");
for (i=0; i
About global and local variables, a GLOBAL variable is declared this way:
关于全局变量和局部变量,GLOBAL变量以这种方式声明:
global_variable='foo'
and a local variable is declared this way:
并以这种方式声明局部变量:
var local_variable='foo'
a Global variable can be declared anywhere in the script and be used anywhere inside the script(and even in other scripts that are attached to the same HTML file ), whereas a Local variable, if declared inside the function, can only be used inside the function, or if you declare it outside the function, it can't be accessed within the function unless you pass the variable to it.
全局变量可以在脚本中的任何位置声明,并可以在脚本内的任何位置使用(甚至可以在附加到同一HTML文件的其他脚本中使用),而Local变量,如果在函数内部声明,则只能在函数,或者如果在函数外声明它,除非将变量传递给函数,否则无法在函数内访问它。
Hope that helps!
希望有所帮助!