简体   繁体   English

使用javascript / jquery从字符串元素中删除内联样式

[英]Remove inline style from string element using javascript/jquery

Suppose I have this string: 假设我有这个字符串:

let string = '<h1 style="lots of class"> </h1><h2> <p style="bunch of class"> </p> <p style="bunch of class"> </p></h2>';

so basically it is a bunch of elements like this but in string: 所以基本上是一堆这样的元素,但是是字符串:

<h1 style="bunch of class"> </h1>
<h2> 
  <p style="bunch of class"> </p>
  <p style="bunch of class"> </p>
</h2>

using jquery or vanilla javascript, how do I remove the style attribute from the string? 使用jquery或vanilla javascript,如何从字符串中删除样式属性?

I have already tried this one: 我已经尝试过这个了:

$(string).find('h1').removeAttr('style');

but it is not working 但它不起作用

There's two potential issues here. 这里有两个潜在的问题。 Firstly, you need to create a jQuery object from string and save a reference to it. 首先,您需要从string创建一个jQuery对象并保存对其的引用。 If you don't do that you'll lose the changes you made to the HTML. 如果不这样做,将会丢失对HTML所做的更改。

Secondly you need to use filter() instead of find() , as there is no single root level node to search from in the HTML string. 其次,您需要使用filter()而不是find() ,因为在HTML字符串中没有要搜索的单个根级别节点。 Try this: 尝试这个:

 let string = '<h1 style="color: red;">H1 Foo</h1><h2 style="color: yellow;">H2 Foo<p style="color: green;">P Foo</p><p style="color: blue;">P Foo</p></h2>'; let $string = $(string); $string.filter('h1').removeAttr('style'); $('body').append($string); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

What you can do is, create a virtual element in the code and place the content in it. 您可以做的是,在代码中创建一个虚拟元素,然后将内容放入其中。 then find the element and remove the attribute: 然后找到元素并删除属性:

 let str = '<h1 style="lots of class">h1</h1><h2> <p style="bunch of class"> h2 > p</p> <p style="bunch of class"> h2 > p </p></h2>'; let $div = $('<div>') $div.html(str).find('h1').removeAttr('style'); $(document.body).append($div.html()) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

If you need to remove all the styles from the string, you might do it like this: 如果您需要从字符串中删除所有样式,则可以这样操作:

 let string = '<h1 style="color: red;">H1 Foo</h1><h2 style="color: yellow;">H2 Foo<p style="color: green;">P Foo</p><p style="color: blue;">P Foo</p></h2>'; $('<div>' + string + '</div>') .find('[style]').attr('style', null) .appendTo('body'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Update 更新
In order to give you what you really wanted, I wrote the following jQuery extension. 为了给您您真正想要的,我编写了以下jQuery扩展。
It strips all attributes from html fragment, except those you want to keep. 它从html片段中剥离所有属性,但要保留的属性除外。
Can be applied to jQuery object. 可以应用于jQuery对象。
Takes an array of attrs to keep (optional). 需要保留一个attrs数组(可选)。
Returns html string like jQuery html() method does. 像jQuery html()方法一样返回html字符串。

 $.fn.extend({ stripHTML: function(keep = []) { return $('<div>') .append(this) .find('*').each(function(i, e) { for (var {name} of [...e.attributes]) { if (!keep.includes(name)) e.removeAttribute(name); } }) .end() .html(); } }); let string = '<h1 Title="h1" style="color: red;">H1 Foo</h1><h2 style="color: yellow;">H2 Foo<p style="color: green;">P Foo</p><p style="color: blue;" >P <a href="foo">Foo</a></p></h2>'; let keep = ['href', 'name', 'value']; // attrs to keep let stripped = $(string).stripHTML(keep); $('body').html(stripped); console.log(stripped); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Create a div and insert string to innerHTML of it. 创建一个div并将字符串插入到它的innerHTML中。 Then select h1 and remove style from it. 然后选择h1并从中删除style

 var string = '<h1 style="color:red">h1</h1><h2 style="color:red">h2</h2>'; var div = document.createElement("div"); div.innerHTML = string; div.querySelector("h1").removeAttribute("style"); document.body.append(div) 

We can achieve this through regex. 我们可以通过正则表达式来实现。

const string = '<h1 style="lots of class"> </h1><h2> <p style="bunch of class"> </p> <p style="bunch of class"> </p></h2>';
const stringNoStyle = string.replace(/style="(.*?)" /g, '')
// <h1> </h1>
// <h2>
//    <p> </p>
//    <p> </p>
// </h2>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM