说,有以下可能:
textNode.appendChild(elementNode);
Run Code Online (Sandbox Code Playgroud)
elementNode指那些nodeType设置为1的那些
textNode指那些nodeType设置为2的那些
生产起来并不容易.
我问这个的原因是我找到了一个函数,它在引用的末尾添加了一个引用链接:
function displayCitations() {
var quotes = document.getElementsByTagName("blockquote");
for (var i=0; i<quotes.length; i++) {
if (!quotes[i].getAttribute("cite")) continue;
var url = quotes[i].getAttribute("cite");
var quoteChildren = quotes[i].getElementsByTagName('*');
if (quoteChildren.length < 1) continue;
var elem = quoteChildren[quoteChildren.length - 1];
var link = document.createElement("a");
var link_text = document.createTextNode("source");
link.appendChild(link_text);
link.setAttribute("href",url);
var superscript = document.createElement("sup");
superscript.appendChild(link);
elem.appendChild(superscript);
}
}
Run Code Online (Sandbox Code Playgroud)
看到最后一行" elem.appendChild(superscript);"哪里elem可以textNode?
我认为很难证明它的原因是因为很难获得指定的访问权限textNode.有没有人能够实现这一目标?
不,文本节点总是叶子.相反,您必须将新节点添加到文本节点的父节点 - 使它们成为文本节点的兄弟节点.
这是我尝试将子项添加到文本节点的示例.
<div id="test">my only child is this text node</div>
<script type="text/javascript">
var div = document.getElementById( 'test' );
var textNode = div.childNodes[0];
var superscript = document.createElement("sup");
superscript.text = 'test';
textNode.appendChild( superscript );
</script>
Run Code Online (Sandbox Code Playgroud)
Firefox给出了错误
未捕获的异常:无法在层次结构中的指定点插入节点(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)
| 归档时间: |
|
| 查看次数: |
1932 次 |
| 最近记录: |