[英]JQuery - How to insert element after parent element?
I try to find if an element is on the page. 我尝试查找页面上是否有元素。 If the element is on the page then find its parent element and insert it after that parent element.
如果该元素在页面上,则找到其父元素并将其插入该父元素之后。
JQuery: jQuery的:
if($('.underline .catIcon').length > 0){
$('.underline .catIcon').each(function(){
$(this).insertAfter($(this).parent().find('h2'));
$(this).css('float','left');
});
}
HTML: HTML:
<h2 class="underline">
<div class="catIcon" style="padding-right: 10px;"><!-- element to move -->
<img class="catIcon" alt="Projects" src="http://www.example.com/storage/images/category/on-site-support56x56.jpg" style="float: left;">
</div>
Projekty
</h2>
<!-- here before text and after h2 tag with class underline -->.text
<a class="link2 ui-link" target="_blank" href="http://www.example.com/cz/cs/22.html">>> info</a>
The h2
is the parent's parent element, so you need to use .closest('h2')
, not .parent().find('h2')
- it looks for a h2
element inside the image's parent( div
) h2
是父项的父元素,因此您需要使用.closest('h2')
,而不是.parent().find('h2')
-它在图像的父项( div
)中查找h2
元素
jQuery(function(){
$('.underline .catIcon').each(function () {
$(this).insertAfter($(this).closest('h2'));
$(this).css('float', 'left');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.