javascript DOM编程艺术笔记——CH3 DOM
2015-09-15 02:40
651 查看
Cha3. DOM
D is for document
The DOM can’t work without a document. When you create a web page and load it in a web browser, the DOM comes to life. It takes the document that you have written and turns it into an object.Objects
objects are bundles of data.Variables associated with an object are called properties of the object.
Functions that can be executed by an object are called methods of the object.
M for model
The DOM represents the web page that’s currently loaded in the browser window. The browserprovides model of the page by node tree.
node
element node(rectangle)
text nodes(rounded rectangle)
attribute nodes (circle)
css
class
<p class="special">This paragraph has the special class</p> <h2 class="special">So does this headline</h2> .special { font-style: italic; } h2.special { text-transform: uppercase; }
id
<ul id="purchases"> #purchases { border: 1px solid white; background-color: #333; color: #ccc; padding: 1em; }
methods
Five very handy DOM methods: getElementById, getElementsByTagName,getElementsByClassName, getAttribute, and setAttribute
getElementById
<p id = "go"> haha </p> <script> var a =document.getElementById("go"); alert(typeof(a)); </script>
getElementsByTagName
get an array of the specific tagnotice that it can only get the
var items = document.getElementsByTagName("li"); for(var i=0;i<items.length;i++){ alert(typeof items[i]); }
getElementsByClassName
document.getElementsByClassName("shoes"); document.getElementsByClassName("shoes cool");//element that has classes shoes **and** cool
getAttribute&setAttribute
getAttributevar items = document.getElementsByTagName("p"); for(var i=0;i<items.length;i++){ var title_text =items[i].getAttribute("title"); if(title) alert(items[i].getAttribute("title")); }
setAttribute
var shopping = document.getElementById("purchases"); shopping.setAttribute("title","a list of goods");
相关文章推荐
- 最简单的可取消多选效果(以从水果篮中挑选水果为例)【jsDEMO】
- [LeetCode][JavaScript]Binary Tree Inorder Traversal
- javascript数组的其他方法
- javascript中使用正则表达式删除前后空格的方法
- JavaScript防止网页被拷贝程序代码——SHOPXX
- js比较大小容易忽略的问题
- JavaScript函数之高阶函数
- 【JavaScript】学习笔记(对象的访问,创建;for-in循环)
- 【JavaScript】DOM之我的初步理解
- JS MD5
- jsdoc块和内联标签
- 遇到的面试题:html和javascript
- javaScript this 详解
- Javascript 中 Array的 sort()和 compare()方法
- js 加载跨域文件 权限问题
- Javascript设计模式
- js 动态dom绑定事件
- JavaScript权威设计--JavaScript对象(简要学习笔记八)
- {welcome to JS} 使用外部脚本
- 一个JSP网上书店项目