Exporter (0) Imprimer
Développer tout
EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

insertBefore method

Inserts an element into the document hierarchy as a child node of a parent object.

Document Object Model (DOM) Level 3 Core Specification, Section 1.4

 

Syntax

object.insertBefore(newChild, refChild)

Parameters

newChild [in]

Type: IHTMLDOMNode

IHTMLDOMNode that specifies the new object to be inserted.

refChild [in, optional]

Type: Variant

Variant that specifies the placement of the new element. If this parameter is specified, the new element will be inserted immediately before this existing child element.

Return value

Type: IHTMLDOMNode

Returns a reference to the element that is inserted into the document.

Standards information

Remarks

Do not specify the refChild parameter when inserting the first child node. If children already exist and you do not specify the refChild parameter, the newChild becomes the last child of the parent object.

This method is accessible at run time. If elements are removed at run time, before the closing tag has been parsed, areas of the document might not render.

Windows Internet Explorer 9. Exceptions are only supported when webpages are displayed in IE9 Standards mode.

Microsoft Internet Explorer 6 and later. The attribute object supports this method.

Examples

The following example shows how to use the insertBefore method to insert a new item into an existing list.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/insertBefore.htm


<script type="text/javascript">
    function insertElement()
    {
        var nod=document.createElement("li");
        oUL1.insertBefore(nod, oLIYellow);
        nod.innerText="Orange";
    }
</script>
</head>
<body>
    <p onclick="insertElement()">Click <strong>HERE</strong> to add an item to the following list.</p>
    <ul id="oUL1">
        <li id="oLIRed">Red</li>
        <li id="oLIYellow">Yellow</li>
        <li id="oLIBlue">Blue</li>
    </ul>
</body>

See also

a
abbr
acronym
address
attribute
b
bdo
big
blockQuote
body
button
caption
center
cite
code
col
colGroup
comment
dd
del
dfn
dir
div
dl
documentType
dt
em
fieldSet
font
form
frameSet
head
hn
html
i
iframe
input type=button
input type=checkbox
input type=email
input type=file
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
ins
kbd
label
legend
li
listing
map
marquee
menu
nextID
object
ol
option
p
plainText
pre
ProcessingInstruction
q
s
samp
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp
About the W3C Document Object Model

 

 

Microsoft réalise une enquête en ligne pour recueillir votre opinion sur le site Web de MSDN. Si vous choisissez d’y participer, cette enquête en ligne vous sera présentée lorsque vous quitterez le site Web de MSDN.

Si vous souhaitez y participer,
Afficher:
© 2014 Microsoft