为img和iframe添加一个div类父项,但blockquotes除外 [英] Add a div class parent to img and iframes with exception for blockquotes

查看:111
本文介绍了为img和iframe添加一个div类父项,但blockquotes除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(。post img)。each(function(){ $(this).wrap('< div class =fwparent/>');}); 
$(。post iframe)。each(function(){$(this).wrap('< div class =fwparent/>');});

用fwparent div类包装我的图像和iframe。但现在我需要他们为blockquotes制作例外。



现在看起来像这样:

 < div class =post> 
...
< div class =fwparent>< img src =image.png>< / div>
< div class =fwparent>< iframe src =movie>< / div>
...
< blockquote>
< div class =fwparent>< img src =image.png>< / div>
< div class =fwparent>< iframe src =movie>< / div>
< / blockquote>
...
< / div>

但我希望它看起来像这样:

 < div class =post> 
...
< div class =fwparent>< img src =image.png>< / div>
< div class =fwparent>< iframe src =movie>< / div>
...
< blockquote>
< img src =image.png>
< iframe src =movie>
< / blockquote>
...
< / div>

有谁知道如何达到这个目的?

  $(。post img, .post iframe)。each(function(){
if($(this).parent()。prop('tagName')!='BLOCKQUOTE'){
$(this).wrap ('< div class =fwparent/>');
}
});

代码检查当前元素的父元素是否是一个blockquote,如果不是,这个元素。该代码还使用道具方法


I'm using these jQuery snippets:

$(".post img").each(function(){ $(this).wrap('<div class="fwparent" />'); });
$(".post iframe").each(function(){ $(this).wrap('<div class="fwparent" />'); });

To wrap my images and iframes with the fwparent div class. But now I need them to make exceptions for blockquotes.

It looks like this now:

<div class="post">
...
<div class="fwparent"><img src="image.png"></div>
<div class="fwparent"><iframe src="movie"></div>
...
<blockquote>
<div class="fwparent"><img src="image.png"></div>
<div class="fwparent"><iframe src="movie"></div>
</blockquote>
...
</div>

But I want it to look like this:

<div class="post">
...
<div class="fwparent"><img src="image.png"></div>
<div class="fwparent"><iframe src="movie"></div>
...
<blockquote>
<img src="image.png">
<iframe src="movie">
</blockquote>
...
</div>

Does anyone know how to achieve this?

解决方案

You can try this code rather :

$(".post img, .post iframe").each(function(){
    if ($(this).parent().prop('tagName') != 'BLOCKQUOTE') {
        $(this).wrap('<div class="fwparent" />');
    }
});

The code checks if the parent of the current element is a blockquote, and if it's not, it wrap this element. The code uses also the prop method.

这篇关于为img和iframe添加一个div类父项,但blockquotes除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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