admin管理员组文章数量:1312908
I want to add a script after a div, this is what I have:
<div id="uno">insert after this</div><br />
<p>this is a paragraph</p><br />
<div>this is a div</div>
var myscript = document.createElement('script');
myscript.setAttribute('src', 'Scripts/start.debug.js');
document.body.appendChild(myscript);
and this code will ad <script src="Scripts/start.debug.js"></script>
at the end of the page, and i need it after div #uno
. What can I use instead of document.body.appendChild
?
I want to add a script after a div, this is what I have:
<div id="uno">insert after this</div><br />
<p>this is a paragraph</p><br />
<div>this is a div</div>
var myscript = document.createElement('script');
myscript.setAttribute('src', 'Scripts/start.debug.js');
document.body.appendChild(myscript);
and this code will ad <script src="Scripts/start.debug.js"></script>
at the end of the page, and i need it after div #uno
. What can I use instead of document.body.appendChild
?
- 3 Why does it need to be after a particular element? The only reason to put script elements in a particular place is when they reference elements during load. Otherwise, they can go pretty much anywhere. – RobG Commented Jun 29, 2011 at 0:43
1 Answer
Reset to default 5<script>
// This function inserts newNode after referenceNode
function insertAfter( referenceNode, newNode )
{
referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
}
insertAfter(document.getElementById("uno"), newScript);
</script>
Even if a nextSibling doesn't exist in the DOM it will still work "because when the second parameter of insertBefore is null then the newNode is appended to the end of the parentNode". A great description can be found here: http://wwwlobo./javascript-insertafter.html.
本文标签:
版权声明:本文标题:html - .insertAfter or .append to an Element Id with Javascript. it's a script tag so it can't be with $ - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741900782a2403856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论