我有这样的 HTML 代码:
<div id="content">
<div class="foobar"></div>
</div>
我想在里面附加子元素div#content
之前div.foobar
元素。怎样才能做到这一点呢?
在回答你的问题之前,让我先让你更清楚、更容易地了解一些事情,以便你明白你想要做什么
Append意味着您可以在选择器的末尾插入元素作为子元素
example append() and appendTo()
Prepend意味着您可以在选择器的开头插入元素作为子元素
example prepend() and prependTo()
请注意,选择器将被视为parent
Before意味着您可以在选择器之前添加元素
example before() and insertBefore()
After意味着您可以在选择器后面添加元素
example after() and insertAfter()
之前和之后does not
将选择器视为parent
element
现在回答你的问题
你可以使用prepend or before
<div id="content">
<div class="foobar"></div>
</div>
$("your element").insertBefore("#content div.foobar") // or
$("#content div.foobar").before("element") // or
$("#content").prepend("element") // or
$("element").prependTo("#content")
注意选择器的位置prepend() and prependTo()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)