jquery in action 学习笔记
1
面對對象的編程
1.引用傳遞
在javascript中,string int Boolean 不是按引用進行傳遞的.而對象和數組是按引用傳遞的.
示例:
?//?Create?an?array?of?itemsvar?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代碼時的一些注意事項:
// Both of these are true
null == false
0 == undefined
// You should use !== or === instead
null !== false
false === false
DOM 編程
<p><strong>Hello</strong> how are you doing?</p>
使用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 <strong> Element
// Non-Mozilla Browsers:
strongElem.innerText
// All platforms:
strongElem.firstChild.nodeValue
獲取元素內的html
? Mozilla-based browsers don’t return the <style> elements in an innerHTML statement.
? Internet Explorer returns its elements in all caps, which if you’re looking for consistency
can be frustrating.
? The innerHTML property is only consistently available as a property on elements of HTML DOM documents; trying to use it on XML DOM documents will result in retrieving null values.
獲取或設置一個元素的屬性值 getAttribute SetAttribute.
Jquery代碼學習
Jquery in action 選擇器部分學習
Ul li a 不管多少層次,會選中所有符合ul li 下的a.不管多少層級.
Ul li>a 只選擇直接孩子的a標簽.
Li:has(a)容器選擇器,選擇包含鏈接的li元素以進行某項操作.
通過位置進行定位的選擇器.
??
Jquery自定義的篩選選擇器:
?//防止重復提交的一個方法 w3c官方推薦的屬性設置 ????????????$("form").submit(function?()?{$(":submit").attr("disabled",?"disabled");
}); ?
轉載于:https://www.cnblogs.com/huaxiaoyao/archive/2011/02/16/1956526.html
總結
以上是生活随笔為你收集整理的jquery in action 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件过程改进之百科名片
- 下一篇: 欢迎光临CAX软件二次开发开源社区!