Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在包含特定元素的第一个元素之前或之后插入?_Jquery_Html - Fatal编程技术网

Jquery 如何在包含特定元素的第一个元素之前或之后插入?

Jquery 如何在包含特定元素的第一个元素之前或之后插入?,jquery,html,Jquery,Html,我有一些HTML(我无法控制)来自数据库,它包含大量文本和图像。我需要在第一个标记之前或之后插入一个特定元素,该标记内部有一个标记 以下是HTML的一个示例: 上述操作将仅在img标记之前插入div,但插入a元素中。我希望它插入a元素之前。这里使用的方法是示例 $(“a”).find('img').first($(.movethisdiv”).insertBefore($(.section.main>a img:first”).parent()) 诺基亚8 4GB RAM 64GB双卡手机

我有一些HTML(我无法控制)来自数据库,它包含大量文本和图像。我需要在第一个
标记之前或之后插入一个特定元素,该标记内部有一个
标记

以下是HTML的一个示例:

上述操作将仅在
img
标记之前插入
div
,但插入
a
元素中。我希望它插入
a
元素之前。

这里使用的方法是示例

$(“a”).find('img').first($(.movethisdiv”).insertBefore($(.section.main>a img:first”).parent())

诺基亚8 4GB RAM 64GB双卡手机

诺基亚8经过40个阶段的严格加工、阳极氧化和抛光过程,确保其独特的设计与抛光铝合金一体机完美匹配

诺基亚8是无缝一体式结构的终极产品,其设计完美贴合手掌

我们提供14天退款保证,以防改变主意

我想在
.first()
之后添加
.parent()
,这样做:
$(“.movethisdiv”).insertBefore($(.section.main”).find(“a>img”).first().parent())

您可以使用过滤器:

$('.movethisdiv')。在之前插入(
$($section.main”).find('a').filter(函数(){//get main中的所有锚点
return$(this).children('img').length;//过滤掉带有图像的锚点
}).first());//获取第一个
.movethisdiv{
颜色:红色;
}

诺基亚8 4GB RAM 64GB双卡手机

诺基亚8经过40个阶段的严格加工、阳极氧化和抛光过程,确保其独特的设计与抛光铝合金一体机完美匹配

诺基亚8是无缝一体式结构的终极产品,其设计完美贴合手掌

我们提供14天退款保证,以防改变主意


我想移动此内容
您可以这样做

查找
a
中的所有
img
,选择第一个,然后选择其父项

然后在父级之前插入div

let firstAnchor=$(“一个img”).first().parent()
$(“.movethisdiv”).insertBefore(firstAnchor)
.movethisdiv{
颜色:红色;
字体大小:20px;
}

诺基亚8 4GB RAM 64GB双卡手机

诺基亚8经过40个阶段的严格加工、阳极氧化和抛光过程,确保其独特的设计与抛光铝合金一体机完美匹配

诺基亚8是无缝一体式结构的终极产品,其设计完美贴合手掌

我们提供14天退款保证,以防改变主意


我不想完全移动此内容。div仍在锚元素中,因此现在已超链接到诺基亚网站。我需要div放在锚元素之前,锚元素里面有一个图像
<section class="main">
<p>Nokia 8 4GB RAM 64GB Dual Sim Phone</p>
<p>The Nokia 8 undergoes a rigorous 40-stage process of machining, anodizing and polishing to ensure its distinctive design pairs flawlessly with the polished aluminium unibody.</p>
<p>The ultimate in seamless unibody construction, Nokia 8 is designed to nestle perfectly in the palm of your hand.</p>
<a href="https://www.nokia.com">
<img src="https://cdn2.gsmarena.com/vv/bigpic/nokia-8-.jpg" />
</a>
<p>We offer 14 days money back guarantee in case of a change of mind. </p>
</section>
<div class="movethisdiv">I want to move this content</div>
$(".movethisdiv").insertBefore( $("section.main").find("a > img").first());