1. 节点分为三类:
  2. 1.元素节点:标签<div></div>
  3. 2.文本节点:标签内的纯文本。
  4. 3.属性节点:标签内的属性,idclass
  5.  
  6. 查找元素:
  7. getElementById();参数传递一个元素的id值,这样就可以获取到该元素节点
  8.  
  9. DOM操作必须等待HTML文档加载完毕,才可以获取
  10.  
  11. 怎样获取?
  12. 1.把<script>移后
  13. HTMLDivElement表示Div的节点对象
  14. IE是以COM实现的DOM,所以只会返回一个object
  15. 2.onload事件来加载html
  16. window.onload = function(){
  17. var box = document.getElementById('box'); //这里存放当网页所有内容都加载完毕后,再执行的代码
  18. alert(box);
  19. }
  20.  
  21. 一个页面只能允许一个id,表示唯一性。
  22. getElementById建议区分大小写,以免不兼容。
  23. 如果是IE5.0-,不兼容getElementById,可以做个判断
  24. window.onload = function(){
  25. if(document.getElementById){
  26. var box = document.getElementById('box');
  27. alert(box);
  28. }else{
  29. alert('您的浏览器不兼容,请更换~');
  30. }
  31. }
  32.  
  33. //元素节点的属性
  34. window.onload = function(){
  35. var box = document.getElementById('box');
  36. alert(box.tagName); //获取这个元素节点的标签名
  37. alert(box.innerHTML); //获取这个元素节点里的文本,纯文本不包含标签
  38. //innerHTMl获取的是这个元素的文本内容,而不是文本节点。
  39. }
  40. window.onload = function(){
  41. var box = document.getElementById('box');
  42. box.innerHTML = 'ssssss<strong>ffff</strong>'; //赋值,可以解析HTML,不是单纯的文本。
  43. }
  44.  
  45. //HTML属性的属性
  46. window.onload = function(){
  47. var box = document.getElementById('box');
  48. alert(box.id); //获取这个元素节点id的属性值,注意不是属性节点
  49. alert(box.title); //获取title属性的值
  50. alert(box.style); //获取style属性对象
  51. alert(box.style.color;) //获取style属性对象中color属性的值
  52. alert(box.className); //获取class属性的值
  53. }
  54.  
  55. //getElementsByTagName()方法将返回一个对象数组HTMLCollection(NodeList),这个数组保存着所有相同元素名的节点列表。
  56. window.onload = function(){
  57. var li = document.getElementsByTagName('li'); //参数传递一个标签名即可
  58. alert(li); //返回一个数组集合,HTMLCollection
  59. alert(li.length); //返回数组的数量
  60. alert(li[0]); //HTMLElement,li的节点对象
  61. alert(li.item(0)); //同上,意义一致。HTMLElement
  62. alert(li[0].tagName); //LI。获得元素的标签名
  63. alert(li[0].innerHTML); //
  64. }
  65.  
  66. //返回body节点
  67. window.onload = function(){
  68. var body = document.getElementsByTagName('body')[0];
  69. alert(body); //返回HTMLBodyElement对象,body节点。
  70. }
  71.  
  72. window.onload = function(){
  73. var all = document.getElementsByTagName('*');
  74. alert(all.length); //火狐浏览器的firebug打开后,会自动创建一个div,所以会多算一个。
  75. alert(all[0].tagName); //IE浏览器比火狐和谷歌多一个节点,是把<!文档声明也算进去了>
  76. }
  77.  
  78. window.onload = function(){
  79. var input = document.getElementsByName('test')[0];
  80. alert(input.value);
  81. alert(input.checked);
  82. };
  83.  
  84. //getAttribute() 方法返回指定属性名的属性值。
  85. window.onload = function(){
  86. var box = document.getElementById('box');
  87. alert(box.getAttribute('style')); //非IE返回的是style字符串,IE返回的是对象,这里有个不兼容
  88. alert(box.getAttribute('bbb')); //自定义可以兼容
  89. alert(box.getAttribute('class')); //IE无法获取
  90. alert(box.getAttribute('className')); //IE可以获取,非IE无法获取
  91.  
  92. //跨浏览器获取className
  93. if(box.getAttribute('className')==null){
  94. alert(box.getAttribute('class'));
  95. }else{
  96. alert(box.getAttribute('className'));
  97. }
  98. };
  99.  
  100. window.onload = function(){
  101. var box = document.getElementById('box');
  102. alert(box.onclick); //均返回函数式;
  103. alert(box.getAttribute('onclick')); //IE7以下会返回函数式,非IE返回字符串
  104. };
  105.  
  106. window.onload = function(){
  107. var box = document.getElementById('box');
  108. //box.setAttribute('title','abc'); //创建一个属性和属性值
  109. //box.setAttribute('align','center');
  110. //box.setAttribute('bbb','cccc');
  111. box.setAttribute('style','color:green'); //IE7以下,style和onclick没有效果,避免使用
  112. };
  113.  
  114. window.onload = function(){
  115. var box = document.getElementById('box');
  116. box.removeAttribute('style'); //IE6一下不支持removeAttribute
  117. };
  118.  
  119. 3.DOM节点
  120. window.onload = function(){
  121. var box = document.getElementById('box');
  122. alert(box.nodeName); //获取元素节点的标签名,和tagName等价
  123. alert(box.nodeType); //获取元素节点的类型值,1
  124. alert(box.nodeValue); //元素节点本身没有内容,所以是null
  125. //node本身把节点指针放在元素<div>上,也就是说,本身是没有value
  126. //如果要输出<div>中里面包含的文本内容,那么前面innerHTML
  127. alert(box.innerHTML); //获取元素节点里面的文本内容
  128. //node只能获取当前节点的东西。
  129. //文本节点 不等于 文本内容
  130. };
  131.  
  132. window.onload = function(){
  133. var box = document.getElementById('box');
  134. alert(box.childNodes); //NodeList集合,返回当前元素节点所有的子节点列表
  135. alert(box.childNodes.length); //3个子节点
  136. //3个子节点为:div<em>sss</em>fefef
  137. //第一个子节点为:div,这个节点称作为:文本节点
  138. //第二个子节点为:<em>sss</em>,这个节点称作为:元素节点
  139. //第三个子节点为:文本节点。
  140. alert(box.childNodes[0]); //Object Text表示一个文本节点对象
  141. alert(box.childNodes[0].nodeType); //3,表示文本节点
  142. alert(box.childNodes[0].nodeValue); //获取文本节点的文本内容
  143. alert(box.childNodes[0].innerHTML); //当前的文本,找不到里面的内容,undefined
  144. alert(box.childNodes[0].nodeName); //#text,文本节点没有标签名
  145. };
  146.  
  147. //通过判断节点类型,来获取不同的输出
  148. window.onload = function(){
  149. var box = document.getElementById('box');
  150. for(var i = 0; i<box.childNodes.length; i++){
  151. if(box.childNodes[i].nodeType === 1){
  152. alert('元素节点:'+box.childNodes[i].nodeName);
  153. }else if(box.childNodes[0].nodeType === 3){
  154. alert('文本节点:'+box.childNodes[i].nodeValue);
  155. }
  156. }
  157. };
  158.  
  159. window.onload = function(){
  160. var pox = document.getElementById('pox');
  161. //pox.innerHTML = 'sssss';
  162. //pox.nodeValue = 'eefefeffef'; //没出错,但没有赋值上,nodeValue必须在当前节点上操作。
  163. pox.childNodes[0].nodeValue = 'dfdfdff'
  164. };
  165.  
  166. window.onload = function(){
  167. var box = document.getElementById('box');
  168. //alert(box.childNodes[0].nodeValue); //获取第一个子节点
  169. //alert(box.childNodes[box.childNodes.length -1].nodeValue); //获取最后一个子节点
  170. };
  171.  
  172. firstChildlastChild属性
  173. window.onload = function(){
  174. var box = document.getElementById('box');
  175. alert(box.firstChild.nodeValue); //获取第一个子节点
  176. alert(box.lastChild.nodeValue); //获取最后一个子节点
  177. };
  178.  
  179. //返回文档对象的根节点
  180. window.onload = function(){
  181. var box = document.getElementById('box');
  182. //alert(box.ownerDocument); //HTMLDocument
  183. //alert(document); //HTMLDocument
  184. alert(box.ownerDocument === document);
  185. };
  186.  
  187. window.onload = function(){
  188. var box = document.getElementById('box');
  189. //alert(box.parentNode); //HTMLBodyElement
  190. //alert(box.firstChild.nextSibling); //HTMLSpanElement
  191. //alert(box.firstChild.nextSibling.nodeName); //下一个同级节点的标签名
  192. //alert(box.lastChild.previousSibling.nodeName); //上一个同级节点的标签名
  193. };
  194.  
  195. //attributes 属性返回指定节点的属性集合,即 NamedNodeMap。
  196. window.onload = function(){
  197. var box = document.getElementById('box');
  198. //alert(box.attributes); //NamedNodeMap,集合数组,保存着这个元素节点的属性列表
  199. //alert(box.attributes.length);
  200. //alert(box.attributes[0]); //Attr,属性节点
  201. //alert(box.attributes[0].nodeType); //2,属性节点的类型值
  202. //alert(box.attributes[0].nodeValue); //box,第一个属性的属性值
  203. //lert(box.attributes[0].nodeName); //id,第一个属性的属性名
  204. //alert(box.attributes['title'].nodeValue); //遍历的时候,是从后向前的
  205. };
  206.  
  207. window.onload = function(){
  208. var box = document.getElementById('box');
  209. alert(box.childNodes.length); //有7个节点,包括了空白节点
  210. };
  211.  
  212. window.onload = function(){
  213. var box = document.getElementById('box');
  214. alert(filterWhiteNode(box.childNodes).length);
  215. };
  216. //忽略空白字符
  217. function filterWhiteNode(node){
  218. var ret=[];
  219. for(var i = 0; i<node.length; i++){
  220. if(node[i].nodeType === 3 && /^\s+$/.test(node[i].nodeValue)){
  221. continue;
  222. }else{
  223. ret.push(node[i]);
  224. }
  225. }
  226. return ret;
  227. }
  228.  
  229. //移除空白字符
  230. function filterWhiteNode(node){
  231. for(var i = 0; i<node.childNodes.length; i++){
  232. if(node.childNodes[i].nodeType === 3 && /^\s+$/.test(node.childNodes[i].nodeValue)){
  233. node.childNodes[i].parentNode.removeChild(node.childNodes[i]);
  234. }
  235. }
  236. return node;
  237. }
  238.  
  239. window.onload = function(){
  240. var box = document.getElementById('box');
  241. var p = document.createElement('p'); //只是创建了一个元素节点p,还没有添加到文档中去
  242. box.appendChild(p); //将新节点p添加到id=box的div里的子节点列表的末尾上
  243.  
  244. var text = document.createTextNode('sdfsdfsdf'); //创建一个文本节点
  245. p.appendChild(text); //把文本添加到p里面
  246. box.appendChild(p);
  247. document.getElementsByTagName('body')[0].appendChild(p);
  248. };
  249.  
  250. window.onload = function(){
  251. var box = document.getElementById('box');
  252. var p = document.createElement('p');
  253. //box.parentNode就是body
  254. box.parentNode.insertBefore(p,box); //在box的父节点添加一个p,就是在box前面添加一个元素节点
  255. };
  256.  
  257. //向后添加节点
  258. window.onload = function(){
  259. var box = document.getElementById('box');
  260. var p = document.createElement('p');
  261. insertAfter(p,box);
  262. };
  263. function insertAfter(newElement,targetElement){
  264. //得到父节点,就是body,但是不能直接使用body,如果层次较多,父节点不一定是body
  265. var parent = targetElement.parentNode;
  266. if(parent.lastChild === targetElement){
  267. alert('ss');
  268. parent.appendChild(newElement,targetElement)
  269. }else{
  270. //span节点的前面添加,可以用insertBefore,span节点可以用nextSibling获取
  271. parent.insertBefore(newElement,targetElement.nextSibling);
  272. }
  273. }
  274.  
  275. //浏览器的兼容
  276. window.onload = function(){
  277. var body = document.getElementsByTagName('body')[0];
  278. if(BrowserDetect.browser == 'Internet Explorer' && BrowserDetect.version <= 7){
  279. var input = document.createElement("<input type=\"radio\" name=\"sex\">");
  280. }else{
  281. var input = document.createElement('input');
  282. input.setAttribute('type','radio');
  283. input.setAttribute('name','sex');
  284. }
  285. body.appendChild(input);
  286. };
  287.  
  288. //replaceChild()方法用新节点替换某个子节点。
  289. window.onload = function(){
  290. var span = document.getElementsByTagName('span')[0];
  291. var em = document.createElement('em');
  292. span.parentNode.replaceChild(em,span);
  293. };
  294.  
  295. //cloneNode()方法克隆所有属性以及它们的值。
  296. window.onload = function(){
  297. var box = document.getElementById('box');
  298. var clone = filterWhiteNode(box).firstChild.cloneNode(true);
  299. //克隆一个节点,如果是true就是把标签内的文本也克隆,如果是flase,只是克隆标签
  300. box.appendChild(clone);
  301. };
  302. //移除空白字符
  303. function filterWhiteNode(node){
  304. for(var i = 0; i<node.childNodes.length; i++){
  305. if(node.childNodes[i].nodeType === 3 && /^\s+$/.test(node.childNodes[i].nodeValue)){
  306. node.childNodes[i].parentNode.removeChild(node.childNodes[i]);
  307. }
  308. }
  309. return node;
  310. }
  311.  
  312. //removeChild()方法删除某个子节点。
  313. window.onload = function(){
  314. var box = document.getElementById('box');
  315. //box.removeChild(filterWhiteNode(box).firstChild); //删除box第一个子节点
  316. box.parentNode.removeChild(box);//删除整个box
  317. };
  318. //移除空白字符
  319. function filterWhiteNode(node){
  320. for(var i = 0; i<node.childNodes.length; i++){
  321. if(node.childNodes[i].nodeType === 3 && /^\s+$/.test(node.childNodes[i].nodeValue)){
  322. node.childNodes[i].parentNode.removeChild(node.childNodes[i]);
  323. }
  324. }
  325. return node;
  326. }
  1. window.onload = function(){
  2. //alert(Node); //IE不支持
  3. //if(xxx.nodeType === 1); //表示判断元素节点
  4. //if(xxx.nodeType === 3); //表示判断文本节点
  5. //alert(Node.ELEMENT_NODE); //表示元素节点的类型值
  6. //alert(Node.TEXT_NODE); //表示文本节点的类型值
  7. //if(xxx.nodeType === Node.ELEMENT_NODE);
  8. //if(xxx.nodeType === Node.TEXT_NODE);
  9. //if(xxx.nodeType === 元素);
  10. //if(xxx.nodeType === 文本);
  11. };
  12. if(typeof Node == 'undefined'){
  13. //创建一个全局Node
  14. window.Node={
  15. ELEMENT_NODE:1,
  16. TEXT_NODE:2
  17. };
  18. }
  19. alert(Node.ELEMENT_NODE);
  20. alert(Node.TEXT_NODE);
  21.  
  22. window.onload = function(){
  23. //alert(document); //HTMLDocument
  24. //alert(document.nodeType); //9
  25. //alert(document.nodeValue); //null
  26. //alert(document.nodeName); //#document
  27. //alert(document.childNodes[0]); //DocumentType.IE理解为注释
  28. //alert(document.childNodes[0].nodeType); //10,IE为8
  29. //alert(document.childNodes[0].nodeName);//火狐为HTML,谷歌为html,IE为#document
  30. //alert(document.childNodes[1]); //HTMLHtmlElement
  31. //alert(document.childNodes[1].nodeType); //1
  32. //alert(document.childNodes[1].nodeName);
  33. };
  34.  
  35. window.onload = function(){
  36. alert(document.documentElement); //HTMLHtmlElement
  37. alert(document.body.nodeName); //获取body
  38. alert(document.doctype); //DocumentType,IE会显示null
  39. };
  40.  
  41. window.onload = function(){
  42. //alert(document.title);
  43. //document.title = 'box';
  44. alert(document.URL);
  45. alert(document.domain);
  46. alert(document.referrer);
  47. alert(document.images.length);
  48. };
  49.  
  50. window.onload = function(){
  51. var box = document.getElementById('box');
  52. var text1 = document.createTextNode('Mr.');
  53. var text2 = document.createTextNode('Lee');
  54. box.appendChild(text1);
  55. box.appendChild(text2);
  56. box.normalize(); //合并同一级别的文本节点
  57. //alert(box.childNodes.length);
  58. alert(box.childNodes[0].nodeValue);
  59. };
  60.  
  61. window.onload = function(){
  62. var box = document.getElementById('box');
  63. box.childNodes[0].splitText(3); //把前三个字符分离成新节点
  64. alert(box.childNodes[0].nodeValue);
  65. };
  66.  
  67. window.onload = function(){
  68. var box = document.getElementById('box');
  69. //box.childNodes[0].deleteData(0,3); //把前三个字符删除
  70. //box.childNodes[0].insertData(0,'Hello!'); //添加字符串
  71. //box.childNodes[0].replaceData(0,2,'Miss'); //替换
  72. //alert(box.childNodes[0].substringData(0,2));
  73. //alert(box.childNodes[0].nodeValue);
  74. };
  75.  
  76. window.onload = function(){
  77. //var box = document.getElementById('box');
  78. //alert(box.firstChild); //Comment
  79. //alert(box.firstChild.nodeType); //8
  80. //alert(box.firstChild.nodeValue); //我是注释
  81. var c = document.getElementsByTagName('!'); //IE支持,其他不支持
  82. alert(c[1].nodeValue);
  83. };
  84.  
  85. window.onload = function(){
  86. if(document.compatMode == 'CSS1Compat'){
  87. alert(document.documentElement.clientWidth);
  88. }else{
  89. alert(document.body.clientWidth);
  90. }
  91. };
  92.  
  93. window.onload = function(){
  94. //document.getElementById('box').scrollIntoView(); //将指定的节点滚动到可见范围内
  95. var box = document.getElementById('box');
  96. //alert(box.childNodes.length);
  97. //alert(box.children.length); //忽略掉了空白节点
  98. alert(box.children[0].nodeName);
  99. };
  100.  
  101. window.onload = function(){
  102. var box = document.getElementById('box');
  103. var p = box.firstChild;
  104. //alert(box.contains(p)); //判断box是不是p的父节点
  105. //火狐旧版本不支持。新版本支持
  106. //safari2.x不支持,3.0以上才支持
  107. //很多更低的版本,提供的两个方案均不支持
  108. //alert(box.compareDocumentPosition(p)>16); //包含关系
  109. alert(contains(box,p));
  110. };
  111. //浏览器兼容
  112. function contains(refNode,otherNode){
  113. if(typeof refNode.contains == 'undefined'){
  114. return refNode.contains(otherNode);
  115. }else if(typeof refNode.compareDocumentPosition == 'function'){
  116. return refNode.compareDocumentPosition(otherNode)>16;
  117. }
  118. }
  119.  
  120. window.onload = function(){
  121. var box = document.getElementById('box');
  122. //alert(box.innerText); //获取文本并过滤HTML,直接删掉。
  123. //box.innerText = '<b>123</b>'; //赋值并转义HTML
  124. alert(box.textContent);
  125. };
  126.  
  127. window.onload = function(){
  128. var box = document.getElementById('box');
  129. //alert(document.getElementById('box').innerHTML); //获取文本(不过滤HTML)
  130. //document.getElementById('box').innerHTML = '<b>123</b>'; //可解析HTML
  131. document.getElementById('box').innerHTML = "<script>alert('')</script>"; //不能执行
  132. };
  133.  
  134. window.onload = function(){
  135. var box = document.getElementById('box');
  136. //alert(box.outerText);
  137. //box.outerHTML = '<b>123</b>';
  138. box.outerText = '<b>123</b>';
  139. alert(document.getElementById('box')); //null表明<div>这个标签被抹去
  140. };

JS—DOM操作的更多相关文章

  1. JS DOM操作(创建、遍历、获取、操作、删除节点)

    创建节点 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="u ...

  2. js dom 操作

    JS的DOM操作   1DOM是文档对象模型,这种模型为树模型:文档是指标签文档,对象是指文档中每个元素:模型是指抽象化的东西. 2间隔与延迟间隔执行一段代码(函数):window.setInterv ...

  3. js——DOM操作(一)

    DOM:Document Object Model  文档对象模型 文档:html页面 文档对象:页面中元素 文档对象模型:定义  为了能够让程序(js)去操作页面中的元素 DOM会把文档看作是一棵树 ...

  4. JS DOM操作 函数 事件 阻止事件冒泡

    一 函数 1.字符串函数 s.tolowerCase( ):    -- 变小写 s.toupperCase( ):   -- 变大写 s.substr( 2 , 8 ):     -- 截取     ...

  5. JS DOM操作(五) Window.docunment对象——操作元素

    定位: var a = document.getElementByIt( "id" ) 同辈元素 var b = a.nextSibling;            -- 找 a ...

  6. JS DOM操作(二) Window.docunment对象——操作样式

    一 对元素的定位 在 js 中可以利用 id.class.name.标签名进行元素的定位 id.class  用在客户端 name  用在服务端 用 id 定位                  -- ...

  7. JS DOM操作(一) 对页面的操作

    DOM ——文档对象模型(Document Object Model)是表示和处理一个HTML或XML文档的常用方法. 在网页上,组织页面(或文档)的对象被组织在一个树形结构中,用来表示文档中对象的标 ...

  8. js dom 操作技巧

    1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...

  9. js DOM操作练习

    1.有如下html,如果用js获得被选中的option的text描述(非value)<select id="select_id">    <option vlue ...

随机推荐

  1. PHP开发-模板的使用

    通过今天晚上半个多小时的赵老师对模板的介绍,大致对模板的使用.结构以及开发模板的思想有了个大致的了解. 为什么要开发模板:模板就是将PHP发过过程中使用到.需要操作的事物封装成对象.以便在使用到的时候 ...

  2. Power BI Premium

    Power BI Premium的设计是为了满足拥有大量数据的大公司的需求.微软已经重新构建了Power BI的架构,以允许大量的“只读”用户.Premium用户还可以利用很多新功能. Power B ...

  3. Java中的package和Import关键字的作用

    一.package关键的作用 1.包的概念 package翻译为包,本质上就是一个文件夹,通过包组织项目结构的过程,就是通过包对应的文件夹管理.java和.class文件的过程. 2.包的作用 通过包 ...

  4. (六--二)scrapy框架之持久化操作

    scrapy框架之持久化操作 基于终端指令的持久化存储 基于管道的持久化存储 1 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过 ...

  5. mybatis基础CURD的学习

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  6. s5pc100开发板linux内核移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y 应用于FSC100开发板 交叉编译工具:arm-cortex_a8-linux-gnueabi-gcc linux-2.6 ...

  7. 集合框架的详解,List(ArrayList,LinkedList,Vector),Set(HashSet,TreeSet)-(14)

    集合详解: /* Collection |--List:元素是有序的,元素可以重复.因为该集合体系有索引. |--ArrayList:底层的数据结构使用的是数组结构.特点:查询速度很快.但是增删稍慢. ...

  8. 三十、CI框架之使用cookies实现用户登录和退出。

    一.在控制器中,写入3个函数.一个是login用于登录,一个是logout用于退出,一个show用来展示界面. login代码如下: logout和showuser函数如下: 二.我们的登录界面 三. ...

  9. Day 5 :ArrayList原理、LinkedList原理和方法和迭代器注意事项

    迭代器在变量元素的时候要注意事项: 在迭代器迭代元素 的过程中,不允许使用集合对象改变集合中的元素个数,如果需要添加或者删除只能使用迭代器的方法进行操作.   如果使用过了集合对象改变集合中元素个数那 ...

  10. Ubuntu下运行python文件

    方法一: 直接> python2.7/python3.6 test.py 方法二: 在文件首行写上  #!/usr/bin/python3 这个是配置的路径 也可以通过 > which p ...