jQuery从所有元素中删除样式属性 [英] jQuery Remove style attributes from ALL elements

查看:135
本文介绍了jQuery从所有元素中删除样式属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何从单个元素中删除属性,现在已经有一些文章了:

  $(div .preview )removeAttr( 风格); 

这是html代码:

 < div class =previewstyle =background-color:white> 
< p>一些文字< span style =font-size:15px;>一些文字< / span>< / p>
多一些代码
< / div>

问题是这将只从预览div中删除样式属性。我想从该div内的所有元素中移除。这只是一个例子,在真实场景中会有更多的html。



有没有简短的方法来做到这一点?



更新:
好​​,我现在知道该怎么做。但是,如果html被发送以作为存储在变量中的字符串运行,该怎么办因此,而不是

  $(div.preview)

我有

  var string ='< div class =预览>< p style ='font-size:15px;'> ....< / p>< div>'

有没有什么方法可以说下面的内容?

  $(string ).find(*)removeAttr( 风格)。 


解决方案

您可以使用通配符选择符 * 来选择 div.preview 下的所有内容。

jsFiddle

  $('div.preview *') .removeAttr( '风格'); 



更新



RE:您的相关问题,如果我们有一个字符串而不是DOM元素。我们可以将它们转换成jQuery对象,如上所述删除属性,然后将它们转换回来(如果需要的话)。

jsFiddle

  //获取元素外部HTML的jQuery扩展
// http://stackoverflow.com/a/2419877/1156119
jQuery.fn.outerHTML =函数(s){
返回s
? this.before(s).remove()
:jQuery(< p>)。append(this.eq(0).clone())。html();
};

var string ='< div class =preview>< p style =font-size:15px;> ....< / p>< div> '
var elements = $(string);
elements.find('*')。removeAttr('style');

var withoutStyles = elements.outerHTML();
alert(withoutStyles);


I know how to remove attribute from a single element, there are already some posts about that:

$("div.preview").removeAttr("style");

This is html code:

<div class="preview" style="background-color:white">
   <p>Some text<span style="font-size:15px;">some text</span></p>
   some more code
</div>

The problem is that this will remove style attribute only from preview div. I want to remove from all elements inside that div. This is just as example, in real scenario there will be more extensive html.

Is there any short way to do that?

UPDATE: Great, I know now how to do it. But what if html is sent to function as string stored in variable. So instead of

$("div.preview") 

I have

var string = '<div class="preview"><p style='font-size:15px;'>....</p><div>'

Is there any way saying something like that the below?

$(string).find(*).removeAttr("style");

解决方案

You can use the wildcard selector * to select everything underneath div.preview.

jsFiddle

$('div.preview *').removeAttr('style');

Update

RE: your related question, if we had a string instead of DOM elements. We could convert them into jQuery objects, remove the attribute as above and then convert them back (if desired).

jsFiddle

// jQuery extension to get an element's outer HTML
// http://stackoverflow.com/a/2419877/1156119
jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};

var string = '<div class="preview"><p style="font-size:15px;">....</p><div>'
var elements = $(string);
elements.find('*').removeAttr('style');

var withoutStyles = elements.outerHTML();
alert(withoutStyles);

这篇关于jQuery从所有元素中删除样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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