我有一些来自数据库的 HTML(我无法控制),它包含大量文本和图像。我需要在其中<a>
包含<img>
标签的第一个标签之前或之后插入一个特定元素。
下面是一个 HTML 示例:
<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>
这是我可以移动元素div
之前的范围,img
但显然它永远不会工作,因为 img 被包裹在一个锚标签中。那么我如何让 jQuerydiv
在第一个包含图像的锚元素之前插入?
$(".movethisdiv").insertBefore( $("section.main").find("a > img").first());
以上将仅div
在img
标签之前插入,但在a
元素内。我希望它在a
元素之前插入。
.parent()
在.first()
这样的事情之后添加:$(".movethisdiv").insertBefore( $("section.main").find("a > img").first().parent());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句