javascript insertAfter alternatives to nextSibling hack?
javascript insertAfter alternatives to nextSibling hack?
我有以下代码:
<div id="container">
<div id="cat1"></div>
<div id="cat2"></div>
<div id="cat3"></div>
</div>
我可以做insertBefore,但没有insertAfter…如果我想在容器中添加另一个cat呢?我真的需要使用nextSibling吗?非常奇怪的是,有一个insertBefore,而一个insertAfter必须使用hack。
还有其他选择吗?function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
insertAfter(document.getElementById("tres"), myscript);
既然可以组合insertBefore
和nextSibling
,就不需要另一个函数了。如果第二个参数为空,insertBefore
的行为与appendChild
类似。
你可以像这样扩展原型链来实现insertAfter
:
Node.prototype.insertAfter = function(n,r) {this.insertBefore(n,r.nextSibling);};
这将允许你调用:
someContainer.insertAfter(newNode, someReference);
请注意,旧版本的IE不喜欢你弄乱内置对象的原型。
您可以使用下面的函数来代替您的方法,您将获得解决方案
function insertAfter(referenceNode, newNode) {
newNode.insertBefore(referenceNode.next());
}
相关文章:
- nextSibling ul li ul
- 使用previousSibling和nextSibling来设置属性
- React.js和丰富的数据网格组件或至少是hack [2015]
- javascript firstChild, nextSibling read-only
- 通过JavaScript CSS添加元素后,hack不起作用
- Hack to keep history.pushState ie8-9
- Javascript this.firstChild.nextSibling在Internet Explorer中不工作
- nextSibling instead of nextElementSibling?
- Using DOM nextSibling
- 在PHP中转义json数据而不使用JS注释hack的正确方法
- “穿越DOM"nextSibling属性不工作.(Javascript)
- 当同级存在时,为什么 nextElementSibling 和 nextSibling 为空
- Magento Site Javascript "d0e3a6" Hack
- javascript insertAfter alternatives to nextSibling hack?
- 动态脚本hack回调
- Setting innerHTML of nextSibling
- CSS hack不支持svg浏览器
- 如何添加CSS Hack专门为IE10
- 抽屉菜单与复选框hack,与链接锚在同一页
- 节点之间的区别是什么?nextSibling和ChildNode.nextElementSibling