【www.gdgbn.com--jquery】
jquery in action 详解
1.引用传递
在网页特效中,string int boolean 不是按引用进行传递的.而对象和数组是按引用传递的.
示例:
// create an array of items
var items = new array("one", "two", "three");
// create a reference to the array of items
var itemsref = items;
// add an item to the original array
items.push("four");
// the length of each array should be the same,
// since they both point to the same array object
alert(items.length == itemsref.length);结果是 true 2.每一个function中都有一个上下文变量arguments,它是一个伪数组(不可以改变).它代表着当前function的参数列表.
在javascript中,变量的作用域是整个function,而不是{}.这点有别于c#等其他语言.
// set a global variable, foo, equal to test
var foo = "test";
// within an if block
if (true) {
// set foo equal to "new test"
// note: this is still within the global scope!
var foo = "new test";
}
// as we can see here, as foo is now equal to "new test"
alert(foo == "new test");
// create a function that will modify the variable foo
function test() {
var foo = "old test";
}
// however, when called, "foo" remains within the scope
// of the function
test();
// which is confirmed, as foo is still equal to "new test"
alert(foo == "new test");// a globally-scoped variable, containing the string "test"
var test = "test";
// you"ll notice that our "global" variable and the test
// property of the the window object are identical
alert( window.test == test );
全局变量可以理解为window的属性.
编写javascript代码时的一些注意事项:
要对使用的变量进行声明.以避免不同的作用域时的冲突.
理解0 false ‘’ undefined null 这些值在javascript里是相同的,都等于false.
// both of these are true
null == false
0 == undefined
// you should use !== or === instead
null !== false
false === false
dom 编程
hello how are you doing?
使用dom的时候,小心一些空白(text)造成的影响,经常使你能找到自己想要的目标元素. function cleanwhitespace( element ) {
// if no element is provided, do the whole html document
element = element || document;
// use the first child as a starting point
var cur = element.firstchild;
// go until there are no more child nodes
while ( cur != null ) {
// if the node is a text node, and it contains nothing but whitespace
if ( cur.nodetype == 3 && ! /s/.test(cur.nodevalue) ) {
// remove the text node
element.removechild( cur );
// otherwise, if it"s an element
} else if ( cur.nodetype == 1 ) {
// recurse down through the document
cleanwhitespace( cur );
}
cur = cur.nextsibling; // move through the child nodes
}
}
使用nodetype属性.
element (nodetype = 1):如li a input select等元素.
text (nodetype = 3): 匹配文本元素
document (nodetype = 9): this matches the root element of a document.
获取元素内的文本内容
需要注意的是innertext在非mozilla浏览器中可以使用,火狐中无法使用.
listing 5-15. getting the text contents of the element
// non-mozilla browsers:
strongelem.innertext
// all platforms:
strongelem.firstchild.nodevalue
获取元素内的html
• mozilla-based browsers don’t return the