jQuery 从所有元素中删除样式属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15705332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Remove style attributes from ALL elements
提问by user1324762
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:
这是html代码:
<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.
问题是这只会从预览 div 中删除样式属性。我想从该 div 内的所有元素中删除。这只是一个例子,在实际场景中会有更广泛的 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
更新:太好了,我现在知道该怎么做了。但是如果 html 被发送到函数作为存储在变量中的字符串呢?所以代替
$("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");
回答by Daniel Imms
You can use the wildcard selector *
to select everything underneath div.preview
.
您可以使用通配符选择器*
来选择下面的所有内容div.preview
。
$('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).
RE:你的相关问题,如果我们有一个字符串而不是 DOM 元素。我们可以将它们转换为 jQuery 对象,删除上面的属性,然后将它们转换回来(如果需要)。
// 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);
回答by Jake Zeitz
try: $("div.preview").children().removeAttr("style");
尝试: $("div.preview").children().removeAttr("style");
or: $("div.preview").find("*").removeAttr("style");
to get grandchildren too.
或:$("div.preview").find("*").removeAttr("style");
也得到孙子孙女。
回答by Aram Kocharyan
$("div.preview,div.preview *")).removeAttr("style")
$("div.preview,div.preview *")).removeAttr("style")
回答by Derek
$("div.preview, div.preview *").removeAttr("style");
回答by Kevin B
Select the div, select it's children, then add the div back to the collection and remove class.
选择 div,选择它的子级,然后将 div 添加回集合并删除类。
$('div.preview').find('[style]').addBack().removeAttr('style')
// replace addBack with andSelf for older jQuery
// if you don't want to remove it from the preview div, just leave addBack off.
however it's probably better to fix the editor rather than fix the results in the long run.
但是,从长远来看,修复编辑器可能比修复结果更好。
回答by Sharlike
You could try this
你可以试试这个
$("div.preview").find('*').removeAttr("style");
To remove style from all child elements of div.preview
从所有子元素中删除样式 div.preview
回答by Rafiq
Try doing this
尝试这样做
$("#frameDemo").contents().find("#div_id").parent('div').removeAttr('onclick');
$("#frameDemo").contents().find("#div_id").children('div').removeAttr('onclick');