Javascript jquery按它选择节';s自身索引
如何选择第三节并在其前面添加新节 之前:Javascript jquery按它选择节';s自身索引,javascript,jquery,html,Javascript,Jquery,Html,如何选择第三节并在其前面添加新节 之前: <article id="container"> <section> a </section> <section> b </section> <section> d </section> <section> e </section> </article> <article id="containe
<article id="container">
<section> a </section>
<section> b </section>
<section> d </section>
<section> e </section>
</article>
<article id="container">
<section> a </section>
<section> b </section>
<section> c </section>
<section> d </section>
<section> e </section>
</article>
A.
B
D
E
之后:
<article id="container">
<section> a </section>
<section> b </section>
<section> d </section>
<section> e </section>
</article>
<article id="container">
<section> a </section>
<section> b </section>
<section> c </section>
<section> d </section>
<section> e </section>
</article>
A.
B
C
D
E
您可以使用.after()
选择器:
$("#container section:eq(1)").after($("<section>cc</section>"));
$(“#容器部分:等式(1)”)。在($((“cc”)之后;
您可以使用.after()
选择器:
$("#container section:eq(1)").after($("<section>cc</section>"));
$(“#容器部分:等式(1)”)。在($((“cc”)之后;
使用eq
$newSection = $("<section>c</section>");
$("#container section").eq(2).before($newSection);
$newSection=$(“c”);
$(“#容器节”).eq(2).before($newSection);
,
使用eq
$newSection = $("<section>c</section>");
$("#container section").eq(2).before($newSection);
$newSection=$(“c”);
$(“#容器节”).eq(2).before($newSection);
,
演示-->
您可以使用或如下所示:
$('c')。在前面插入('container>部分:等式(2)')代码>
A.
B
D
E
您可以使用或,如下所示:
$('c')。在前面插入('container>部分:等式(2)')代码>
A.
B
D
E
使用和选择第三部分
//创建一个新的普通元素
var sp1=document.createElement(“节”);
sp1.innerHTML=“c”;
//在插入元素之前,获取对元素的引用
var sp2=document.querySelector(#容器节:第n个子项(3)”//第三节
//获取对父元素的引用
var parentDiv=sp2.parentNode;
//在sp2之前将新元素插入DOM
parentDiv.insertBefore(sp1,sp2)代码>
A.
B
D
E
使用和选择第三部分
//创建一个新的普通元素
var sp1=document.createElement(“节”);
sp1.innerHTML=“c”;
//在插入元素之前,获取对元素的引用
var sp2=document.querySelector(#容器节:第n个子项(3)”//第三节
//获取对父元素的引用
var parentDiv=sp2.parentNode;
//在sp2之前将新元素插入DOM
parentDiv.insertBefore(sp1,sp2)代码>
A.
B
D
E
从技术上讲,这是不正确的。他想在第三个元素之前添加一个新的部分。所以$(“#文章部分:等式(2)”)之前($("ccIsn不在第三个元素之前意味着在第二个元素之后??如果它们之间有任何东西,比如div或其他东西,那么它将是列表中的一个元素。好吧,在这个例子中没有,但我猜他不会在他的项目中使用这个确切的例子。好吧,从技术上讲,这是不正确的。他想在t之前添加一个新的部分hird元素。因此,$(“#文章部分:eq(2)”)。before($)(“第三个元素之前的ccIsn不是指第二个元素之后??如果它们之间有任何东西,比如div或其他东西,那么它将是列表中的一个元素。好吧,在这个示例中不是,但我猜他不会在他的项目中使用这个确切的示例。