2016-09-06 87 views
0

我有这个代码创建了一个元素,但是我面对的问题是它将它追加到页面的最底部,为了这个工作我需要它被定位在DOM中的某个地方,我该怎么做?将元素放置在DOM中的某个地方JavaScript

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); 
} 
+0

你必须知道容器中添加元素.. – Rayon

+0

我知道它包含在这样做,我只是将其追加到你们? –

+0

要定位添加元素作为孩子检查此: http://stackoverflow.com/questions/5882768/how-to-append-a-childnode-to-a-specific-position –

回答

0
afterWhichNode.parentNode.insertBefore(newNode, afterWhichNode.nextSibling); 

afterwichNode此代码后会插入一个节点,那是用香草的JavaScript,如果你正在使用jQuery,只需使用.after()

0

目前要附加的元素在体内的标签,它将永远处于底部。所以如果你想在特定位置追加元素,你必须将它追加到该容器中。让说你要追加它在“POS”,你可以这样做:

pos.appendChild(elem); 
+0

谢谢它的工作原理,我之前做过但我仍然保留文档,所以它是document.pos.appendChild。 –

相关问题