关于HTML的Element

今天搞HTML的时候,发现了一些操作element的方法。先引用一篇。

1.document.getElementById(id);  
2.document.getElementByTagName(tagName);

3.element.childNodes 
   --element.firstChild=element.childNodes[0]; 
  --element.lastChild=element.childNodes[element.childNonts.length-1]; 
4.element.parentNode

5.element.nextSibling; //引用下一个兄弟结点 
element.previousSibling; //引用上一个兄弟结点

6.nodeName属性获得结点名称 
  --对于元素结点返回的是标记名称,如:<a herf><a>返回的是"a" 
  --对于属性结点返回的是属性名称,如:class="test" 返回的是test 
  --对于文本结点返回的是文本的内容 
7.nodeType返回结点的类型 
  --元素结点返回1 
  --属性结点返回2 
  --文本结点返回3 
8.nodeValue返回结点的值 
  --元素结点返回null 
  --属性结点返回undefined 
  --文本结点返回文本内容

9.hasChildNodes()

10.tagName

11.每个属性结点都是元素结点的一个属性,可以通过(元素结点.属性名称)访问 
12 setAttribute()   

  --elementNode.setAttribute(attributeName,attributeValue);

13.使用getAttribute()方法

  --elementNode.getAttribute(attributeName); 
 
14.innerHTML和innerText属性

<script language"javaScript" type="text/javascript"> 
function cleanWhitespace(element) 

for(var i=0; i<element.childNotes.length; i++) 

var node = element.childNodes[i]; 
if(node.nodeType == 3 && !/\S/.test(node.nodeValue)) 

node.parentNode.removeChild(node); 



</script>

15.document.createElement()方法创建元素结点 
  --如:document.createElement("Span"); 
16.document.createTextNode()方法创建文本结点 
  --如:document.createTextNode(" ");

17.使用appendChild()方法添加结点 
18.使用insertBefore()方法插入子节点 
 
19.使用replaceChild方法取代子结点 
 
20.使用cloneNode方法复制结点 
  --node.cloneNode(includeChildren); 
  --includeChildren为bool,表示是否复制其子结点 
21.使用removeChild方法删除子结点

22.添加行和单元格 
var _table=document.createElement("table"); //创建表 
table.insertRow(i); //在table的第i行插入行 
row.insertCell(i); //在row的第i个位置插入单元格 
23.引用单元格对象 
-  -table.rows[i].cells[i]; 
24.删除行和单元格 
  --table.deleteRow(index); 
  --row.deleteCell(index); 
25.交换两行获得两个单元格的位置 
  node1.swapNode(node2);

然后再讲自己的验证,搬来代码如下,以下内容运行于IE9,Chrome均可:

<script type="text/javascript">
    function displayKeisaiMeisai(img){
        var tr = img.parentNode.parentNode;
        var tr1 = tr.getElementsByTagName("TD")[1].getElementsByTagName("TABLE")[0].getElementsByTagName("TR")[1];
        if(tr1.style.display=="none"){
            tr1.style.display="block";
            img.src = "./img/triangle_displayNone.png";
        } else {
            tr1.style.display="none";
            img.src = "./img/triangle_dispaly.png";
        }
    }

    function addRow2() {
        var _tab = document.getElementById("keisai_b");
        var newnode = _tab.getElementsByTagName("TR")[0].cloneNode(true);
        _tab.getElementsByTagName("TBODY")[0].appendChild(newnode);
    }

</script>

<input type="button" value="Test追加挿入"
                style="height: 35px; width: 150px; margin-top: 3px; margin-left: 0px;"
                id="btnAdd"
                onClick="addRow2();" />
<table id = "keisai_b">
    <tr>
        <td>
            <table name="keisai_b_node">
                <tr>
                    <td style="vertical-align:top;">
                        <img id="keisai_b_1" style="margin-top: 5px;" src="./img/triangle_dispaly.png" onclick="displayKeisaiMeisai(this);"/>
                    </td>
                    <td>
                        <table style="background-color: white;">
                            <tr>
                                <td style="border:solid 1px #FF6600; width:700px;"><div>決裁</div></td>
                            </tr>
                            <tr id="keisai_b_1_m" style="display:none;">
                                <td>
                                <table class="mazarine" style="background-color: #FF6600; border: solid 2px #FF6600;  ">
                                    <tr>
                                    <th style="border-color: white; background-color: #FF6600;">
                                        <div style="width: 160px;">決裁</div></th>
                                    <td style="text-align: left; border-color: #FF6600; background-color: white;">
                                        <div style="width: 120px;"></div></td>
                                    <th style="border-color: #FF6600; background-color: #FF6600;">
                                        <div style="width: 80px;">決済日</div></th>
                                    <td style="width: 100px; text-align: left; border-color: #FF6600; background-color: white;">
                                        <div style="width: 120px;"></div></td>
                                    <th style="width: 80px; border-color: #FF6600; background-color: #FF6600;">
                                        <div style="width: 80px;">決裁者</div></th>
                                    <td style="width: 100px; text-align: left; border-color: #FF6600; background-color: white;">
                                        <div style="width: 120px;"></div></td>
                                    </tr>
                                    <tr>
                                        <th style="border-color: white; background-color: #FF6600;">
                                            <div style="width: 160px;">連絡事項</div></th>
                                        <td colspan="5" style="text-align: left; border-color: #FF6600; background-color: white;"></td>
                                    </tr>
                                </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
时间: 2024-10-28 23:42:46

关于HTML的Element的相关文章

215. Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note:You may assume k is always valid, 1 ≤ k ≤ array's le

LeetCode OJ 162. Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

LeetCode(7): Majority Element

Majority Element: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always exist in the array. 题意:找出给定数组中的

[LeetCode]Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: Special thanks to @

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

Bentley GeoStructural Finite Element Analysis(FEM) v17.00.33.00 1CD

Systat.PeakFit.v4.12.00 1CD Autodesk.CADDoctor.For.Autodesk.Simulation.v2015.Win64-ISO 1DVD Autodesk.Vault.Basic.v2015-ISO 1DVD Autodesk.Vault.Workgroup.v2015-ISO 1DVD Command.Digital.AutoHook.2015.v0.8.0.60.beta.1 1CD OmniCAD.v1.0.0.2125.for.Siemens

- Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead. 解决方案

<template> <div>{{hello}}</div> <button @click="addOne">add one</button> <button @click="minusOne">minus one</button> </template> 在*.vue组件里有这么一段. 报错信息: (Emitted value instead of an instan

EventTarge Node Docuement Element HTMLElement 关系

综述: 可以将其看做是依次继承的关系: Node Node A Node is an interface from which a number of DOM types inherit, and allows these various types to be treated (or tested) similarly(好几种dom类型都继承自node接口,这些dom类型对外会表现的很相似). The following interfaces(下面的这些接口都继承自Node的方法和属性,包括D

162. Find Peak Element (Array; Divide-and-Conquer)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi