jQuery Remove style attributes from ALL elements

前端 未结 7 527
挽巷
挽巷 2020-12-29 07:52

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

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

Th

相关标签:
7条回答
  • 2020-12-29 08:03

    try: $("div.preview").children().removeAttr("style");

    or: $("div.preview").find("*").removeAttr("style"); to get grandchildren too.

    0 讨论(0)
  • $("div.preview,div.preview *")).removeAttr("style")

    0 讨论(0)
  • 2020-12-29 08:13

    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);
    
    0 讨论(0)
  • 2020-12-29 08:16
    $("div.preview, div.preview *").removeAttr("style");
    
    0 讨论(0)
  • 2020-12-29 08:18

    Try doing this

    $("#frameDemo").contents().find("#div_id").parent('div').removeAttr('onclick');
    $("#frameDemo").contents().find("#div_id").children('div').removeAttr('onclick');
    
    0 讨论(0)
  • 2020-12-29 08:21

    Select the div, select it's children, then add the div back to the collection and remove class.

    $('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.

    0 讨论(0)
提交回复
热议问题