English 中文(简体)
如何将DOM要素定为第一儿童?
原标题:How to set DOM element as first child?

我有一个要素E,我用一些内容。 所有这一切都突然发生,我发现,传来的下一个要素应该是E的第一个孩子。 是什么骗子,如何做? 由于E是一个目标,而不是阵列,因此采用非临时性的固定工作方式。

漫长的办法是通过爱婴孩进行消化,并搬进钥匙++,但我确信,有预想办法。

最佳回答
var eElement; // some E DOM instance
var newFirstElement; //element which should be first in E

eElement.insertBefore(newFirstElement, eElement.firstChild);
问题回答

2018 version - prepend

parent.prepend(newChild)  // [newChild, child1, child2]

这是现代的联邦文件! 这比以往的备选办法更可读。 目前可在 Chrome、FF和歌舞厅查阅。

添加到末尾的对应数字为append,取代原来的appendChild

parent.append(newChild)  // [child1, child2, newChild]

Advanced usage

  1. You can pass multiple values (or use spread operator ...).
  2. Any string value will be added as a text element.

<>>Examples:

parent.prepend(newChild, "foo")   // [newChild, "foo", child1, child2]

const list = ["bar", newChild]
parent.append(...list, "fizz")    // [child1, child2, "bar", newChild, "fizz"]

Related DOM methods

  1. Read More - child.before and child.after
  2. Read More - child.replaceWith

Mozilla 文件

https://caniuse.com/#feat=dom-manip-convenience”rel=“noreferer”

You can implement it directly i all your window html elements.
Like this :

HTMLElement.prototype.appendFirst = function(childNode) {
    if (this.firstChild) {
        this.insertBefore(childNode, this.firstChild);
    }
    else {
        this.appendChild(childNode);
    }
};

接受的答复,后附:

function prependChild(parentEle, newFirstChildEle) {
    parentEle.insertBefore(newFirstChildEle, parentEle.firstChild)
}

除非我误解:

$("e").prepend("<yourelem>Text</yourelem>");

$("<yourelem>Text</yourelem>").prependTo("e");

虽然从你描述中可以看出,存在某种条件,

if (SomeCondition){
    $("e").prepend("<yourelem>Text</yourelem>");
}
else{
    $("e").append("<yourelem>Text</yourelem>");
}

我创建了这一原型,以向母体元素预支元素。

Node.prototype.prependChild = function (child: Node) {
    this.insertBefore(child, this.firstChild);
    return this;
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.