// 1.通过获取dom方式直接获取子节点实列varp =document.getElementById('childId')// 获取元素后利用getElementsByTagNameconsole.log(p.getElementsByTagName('div'))document.write(p.getElementsByTagName('div')) 结果如下:HTMLCollection(5) [div.grandson, div.firstsibling, div.secondsibling, div.threes...
var firstChild=getFirstChildNode(box); console.log(firstChild.nodeName); //p //通过判断nodeType的值是否为1,如果是则为元素节点,否则跳过。 //获取当前元素节点的第一个子节点,过滤空白文本节点 function getFirstChildNode(box){ if(box.firstChild.nodeType!=1){ return box.firstChild.nextSibling; }e...
Code varoU=document.getElementById("myList"); alert(oU.firstChild.nodeType) 进行测试时候,发现: 在IE当中结果都是1; 而在FF中第一个是:1,第二个则是3; 这个问题出在:因为 Firefox 会把无意义的空格和回车也算成一个文本节点。
1-1 getElementById getElementById是通过标签的id名称来获取标签的 因为在一个页面中id是唯一的,所以获取到的就是一个元素 var box = document.getElementById('box') console.log(box) // 获取到的就是页面中的那个id 为 box 的 div 标签 1-2 getElementsByClassName getElementsByClassName是用过标...
console.log(firstChild.nodeName); //p //通过判断nodeType的值是否为1,如果是则为元素节点,否则跳过。 //获取当前元素节点的第一个子节点,过滤空白文本节点 function getFirstChildNode(box){ if(box.firstChild.nodeType!=1){ return box.firstChild.nextSibling; ...
因为对象包含属性和行为。 标记型文档包含标签、属性、标签中封装的数据。只要是标记型文档,DOM这种技术...
firstChild 获取指定节点的第一个子节点 lastChild 获取指定节点的最后一个子节点 例如以下代码:box1box2box3varparentElement=document.getElementById('parent')console.log(parentElement.firstChild)console.log(parentElement.lastChild)/* childNodes属性返回的是NodeList集合(动态集合) */varchildren=parent...
getElementById("demo").innerHTML = x; 复制尝试一下 在这个例子中,我们演示了空格如何干扰这个属性。获取元素的第一个子节点的节点名称: First child Last Child var x = document.getElementById("myDIV").firstChild.nodeName; document.getElementById("demo").innerHTML = x; 复制尝试...
从上面的描述中,发现firstChild属性和lastChild属性更加的语义化,而且代码更加的简洁,方便我们记忆; 注意:firstChild和lastChild只能对单个节点就行操作,不能对节点数组进行操作; 2.nodeValue属性 作用:如果我们想改变一个文本节点的值,那就是用DOM提供的nodeValue属性,它是用来得到(和设置)一个文本节点的值; ...
To get the first child node of an HTML element, you can use the firstChild property. This property returns a node's first child, as a Node object. Let us say you have the following HTML code snippet: JavaScript Node Java Ruby Rust The following example selects the first element b...