jQuery:在子元素之前追加子元素 [英] jQuery: Append child element before child element

查看:589
本文介绍了jQuery:在子元素之前追加子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的HTML代码:

I have an HTML code like this:

<div id="content">

    <div class="foobar"></div>
</div>

我想在 div #content内容中附加子元素 div.foobar 元素之前。怎么办呢?

I want to append child elements inside div#content before the div.foobar element. How can do that?

推荐答案

在我回答你的问题之前,让我让你更清楚,更容易,让你明白什么您尝试

Before I answer your question let me make something more clearer and easier to u so that you understand what your trying to

追加表示您可以在选择器的末尾插入元素作为子元素

Append means you can insert elements as child elements at the end of the selector

示例 append()和appendTo()

Prepend 表示您可以在选择器的开头插入元素作为子元素

Prepend means you can insert elements as child elements at the beginning of the selector

示例 prepend()和prependTo()

请注意,选择器将被视为父级

Notice that selector will be considered as a parent

之前表示您可以在选择器之前添加元素

Before means you can add elements just before the selector

示例 before()和insertBefore()

之后表示您可以在选择器之后添加元素

After means you can add elements just after the selector

示例 after()和insertAfter()

之前和在之后将选择器视为元素

before and after does not treat selector as a parent element

现在回答你的问题

你可以使用 prepend或之前

<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()和prependTo()

这篇关于jQuery:在子元素之前追加子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆