简体   繁体   English

将元素放置在DOM JavaScript中的特定位置

[英]Position an element in certain place in DOM JavaScript

I have this code which creates an element but the problem i am facing is it appends it at the very bottom of the page, in order for this to work i need it to be positioned in a certain place in the DOM how can i do this ? 我有这段代码可以创建一个元素,但是我面临的问题是它将它附加在页面的最底部,为了使其正常工作,我需要将其放置在DOM中的某个位置,我该如何做呢? ?

var x  = document.getElementById('options_10528_1');
var pos = document.getElementById('options-10528-list');
x.onclick = function(){
var elem = document.createElement('div');
elem.setAttribute("class","uk-warning");
elem.innerHTML = "Unfortunately, we cannot supply this medicine. Please  consult your GP.";
document.body.appendChild(elem);
}
afterWhichNode.parentNode.insertBefore(newNode, afterWhichNode.nextSibling);

此代码将在afterwichNode之后插入一个节点,即使用普通javascript,如果您使用的是jquery,则只需使用.after()

Currently you are appending the element in the body tag, it will always goes at bottom. 当前,您将元素添加到body标签中,它将始终位于底部。 So if you want to append the element in a specific position, you have to append it in that container. 因此,如果要将元素附加到特定位置,则必须将其附加到该容器中。 let say you want to append it in "pos", you can do this: 假设您要将其附加在“ pos”中,则可以执行以下操作:

pos.appendChild(elem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM