简体   繁体   English

如何使用Javascript在其他元素之前附加元素?

[英]How to append an element before another using Javascript?

I tried following the a wiki and looking into multiple questions here but I still have problems with insertBefore.. 我尝试遵循wiki,并在这里调查了多个问题,但是insertBefore仍然有问题。

This is my sample: 这是我的示例:

<div id="container">
 <span id="first">1</span>
 <span id="second">2</span>
 <div id="third">3</div>
 <div id="forth">4</div>
</div>

<script>
topbar = document.getElementsById("container");
boardlist = document.getElementsById("first");

bmcontainer = document.createElement("span");
bmcontainer.setAttribute("id", "zero");
bmcontainer.innerHTML("0");

topbar.inserBefore(bmcontainer, boardlist);
</script>

I want to append the span#zero before the span#first. 我想在span#first之前附加span#zero。 What am I doing wrong? 我究竟做错了什么? I'm trying to not use jQuery, so I'm looking for a totally javascript solution. 我试图不使用jQuery,所以我正在寻找一种完全JavaScript的解决方案。

Prepend pure javascript 前置纯JavaScript

MDN article on insertBefore() 有关insertBefore()的MDN文章

var el = document.getElementById('thingy'),
    elChild = document.createElement('div');
elChild.innerHTML = 'Content';

// Prepend it
el.insertBefore(elChild, el.firstChild);

Source: http://clubmate.fi/append-and-prepend-elements-with-pure-javascript/#Prepend 来源: http : //clubmate.fi/append-and-prepend-elements-with-pure-javascript/#Prepend

Also, the jQuery prepend() Method 另外,jQuery prepend()方法

You can read about it here and here . 您可以在这里这里阅读有关它的信息

Node.insertBefore() is the answer. Node.insertBefore()是答案。

var insertedNode = parentNode.insertBefore(newNode, referenceNode);

If you have Parent Node , then you should use ParentNode.prepend() 如果您有Parent Node ,那么您应该使用ParentNode.prepend()

ParentNode.prepend(nodesToPrepend);

Take a look @ mentioned links for full documentation and examples. 查看@提到的链接以获取完整的文档和示例。

Update 更新资料

You have some issues in your code ( typo and wrong usage of innerHTML ), here is fixed code. 您的代码中有一些问题( innerHTML错字和错误用法 ),这是固定的代码。

HTML 的HTML

<div id="container">
    <span id="first">1</span>
    <span id="second">2</span>
    <div id="third">3</div>
    <div id="forth">4</div>
</div>

JS JS

var topbar = document.getElementById("container"),
    boardlist = document.getElementById("first"),
    bmcontainer = document.createElement("span");

bmcontainer.setAttribute("id", "zero");   
bmcontainer.innerHTML = "0"; // innerHTML is not a function, it's a property

topbar.insertBefore(bmcontainer, boardlist);

jsfiddle jsfiddle

Notice that InnerHTML is not a function , it's a property , read more about that here: Element.innerHTML 注意, InnerHTML 不是一个function ,它是一个property ,有关更多信息,请参见Element.innerHTML

You can use the insertAdjacentElement function. 您可以使用insertAdjacentElement函数。

var boardlist = document.getElementById("first"),
    bmcontainer = document.createElement("span");
    bmcontainer.setAttribute("id", "zero");

    boardlist.insertAdjacentElement('beforebegin', bmcontainer);

Element.insertAdjacentElement Element.insertAdjacentElement

暂无
暂无

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

相关问题 我应该如何使用Javascript将新创建的元素追加到另一个元素的倒数第二个位置? - How should I append a newly created element to the next to last position of another element using Javascript? 如何使用JavaScript在数据列表中添加元素 - how to append element in datalist in form using javascript 如何使用 javascript 来 append / 添加元素 onclick? - How to append / add a element onclick using javascript? 在附加到另一个元素之前,如何使用Jquery / Javascript从选择中删除一个元素? - how to remove an element from a selection using Jquery/Javascript before appending to another element? 如何使用 JS join() 在数组元素之前 append 自定义字符串 - How to append a custom string before element of array using JS join() 如何使用 javascript 或 jquery 查找元素和 append 新元素 - How to find element and append new element using javascript or jquery 如何将 append 值转换为元素 javascript 以使用 selenium 与元素交互 - How to append value into an element javascript to interact with an element using selenium 如何使用Javascript找到:before元素的位置? - How to find the position of :before element using Javascript? 如何使用JavaScript在锚点之前插入元素 - how insert element before anchor using javascript 如何检测(尝试 append 创建的元素时)该元素之前是否已使用 JavaScript 创建过一次 - How to detect (when trying to append a created element) if that element has been created once before with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM