【DOM编程艺术】显示"文献来源链接表"

博客指出使用document.getElementsByTagName('blockquote').lastChild查找最后一个元素节点时,因部分浏览器会将换行符解释为文本节点,导致该属性指向文本节点而非元素节点。解决方法是查找所有元素节点,取最后一个,即document.getElementsByTagName('blockquote').getElementsByTagName('*')[length - 1]。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>explaining</title>
</head>

<body>
<h1>What is the Document Object Model</h1>
<p>The <abbr title='World Wide Web Consortium'>W3C</abbr> defines the <abbr title='Document Object Model'>DOM</abbr> as:</p>
<blockquote cite="http://www.w3.org/DOM/">
<P>
A platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content,structure the style of document.</P>
</blockquote>
<p>
It is an <abbr title='Application Programming Interface'>API</abbr>
that can be used to navigate <abbr title='HyperText Markup Language'>HTML</abbr> and <abbr title="eXtensible Markup Language">XML</abbr> documents.
</p>
<script>
addLoadEvent(displayCitations);
function displayCitations(){
    if( !document.getElementsByTagName || !document.getElementById || !document.createTextNode ) return false;
    var quotes=document.getElementsByTagName('blockquote');
    for(var i=0;i<quotes.length;i++){
        if( !quotes[i].getAttribute('cite')) continue;   //使用continue立刻跳到下一次循环
        var url=quotes[i].getAttribute('cite');
        var quoteElements=quotes[i].getElementsByTagName('*');
        if( quoteElements.length < 1) continue;
        var elem=quoteElements[quoteElements.length-1];
        var link=document.createElement('a');
        link.setAttribute('href',url);
        var link_text=document.createTextNode('source');
        link.appendChild(link_text);
        var superscript=document.createElement('sup');
        superscript.appendChild(link);
        elem.appendChild(superscript);
        console.log(elem);
    }
}
function addLoadEvent(func){
    var oldEvent = window.onload;
    if( typeof window.onload != 'function'){
        window.onload=func;
    }else{
        window.onload=function(){
             oldEvent();
            func();
        }
    }
}
</script>
</body>
</html>

解析:

<blockquote cite="http://www.w3.org/DOM/">
<P>
A platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content,structure the style of document.</P>
</blockquote>

结果:<TextNode textContent="\n">  

找到blockquote下的块级元素,若用document.getElementsByTagName('blockquote').lastChild  乍看起来,是查找到最后一个元素

事实上,那个p节点的确是blockquote元素的最后一个元素节点,但在</p>和</blockquote>标签之间还存在一个换行符。有些浏览器会

把这个换行符解释为一个文本节点。这样一 来,blockquote元素节点的lastChild属性就将是一个文本节点而不是元素节点。

解决方法:查找所有的元素节点,最后一个元素节点即为p

document.getElementsByTagName('blockquote').getElementsByTagName('*')[length-1]

转载于:https://www.cnblogs.com/positive/p/3675726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值