【问题标题】:How to remove all attributes from html?如何从html中删除所有属性?
【发布时间】:2014-01-27 00:14:26
【问题描述】:

我有原始的 html,里面有一些用于各种标签的 css 类。

例子:

输入:

<p class="opener" itemprop="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque molestias natus iste labore a accusamus dolorum vel.</p>

我只想得到普通的 html,例如:

输出:

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque molestias natus iste labore a accusamus dolorum vel.</p>

我不知道这些类的名称。我需要在 JavaScript (node.js) 中执行此操作。

有什么想法吗?

【问题讨论】:

  • 为什么 HTML 有这些类 - 它是从 CMS 或类似的生成的,如果不是,可以从源中删除吗?
  • 我想建议您将标题更改为“如何从 HTML 标记中删除所有属性?”因为它实际上似乎与“css 引用”无关,不管它们是什么。
  • 在示例中,itemprop="description" 不是 CSS 属性,而是 HTML 元素属性。我猜你想寻找某种 HTML 解析器,因为 HTML 元素有时需要属性(例如 &lt;link&gt;&lt;attr&gt;)。
  • 您需要一个 HTML 解析器来将该字符串转换为节点并对其进行解析,例如 cheerio
  • itemprop 不是 CSS 类。您只是想删除所有属性吗?此外,某些类可能不仅用于 CSS

标签: javascript html node.js text-processing text-parsing


【解决方案1】:

正如我在 cmets 中所指出的,这可以通过 Cheerio 完成。
要删除所有元素的所有属性,您可以:

var html = '<p class="opener" itemprop="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Neque molestias natus iste labore a accusamus dolorum vel.</p>';

var $ = cheerio.load(html);   // load the HTML

$('*').each(function() {      // iterate over all elements
    this.attribs = {};     // remove all attributes
});

var html = $.html();          // get the HTML back

【讨论】:

  • 完美,太棒了!万分感谢。 :)
  • 在删除 [0] 之前无效。即this.attribs = {};
  • 我想这取决于 this 是什么,但三年前我写这篇文章时,它确实与 Cheerio 中的 [0] 一起工作。
  • @CarsonIp 是的,删除 [0] 使其工作,非常感谢
【解决方案2】:

我将使用标签名称和该元素的innerHTML 创建一个新元素。然后你可以用新元素替换旧元素,或者用newEl 做任何你喜欢的事情,如下面的代码:

// Get the current element
var el = document.getElementsByTagName('p')[0];

// Create a new element (in this case, a <p> tag)
var newEl = document.createElement(el.nodeName);

// Assign the new element the contents of the old tag
newEl.innerHTML = el.innerHTML;

// Replace the old element with newEl, or do whatever you like with it

【讨论】:

  • 如何在没有 DOM 的情况下创建元素,这就是 Node ?
  • 谢谢,这个解决方案实际上是你的,所以谢谢! :)
【解决方案3】:

也许 js 中的一些正则表达式可以提取出那些 css 标签,然后输出精简后的版本?如果我理解你的问题,那就是正确的

【讨论】:

【解决方案4】:

也许,只需使用 Notepad++,快速的“查找/替换”操作和空格将是最快的方法,而不是在解析器或类似的东西中思考。

【讨论】:

  • 您的 Find 搜索会是什么?
  • 你是对的,马特。如果他需要以编程方式进行并且他不知道类的名称,那么我的方法是不正确的。
  • adeneo 答案是我认为更好的答案。 +1
  • 谢谢,但现在可以使用的是 JavaScript(最好的 node.js 程序)。
【解决方案5】:

即兴发挥:

$('.some_div').each(function(){
    class_name = $(this).attr('class');
    $(this).removeClass(class_name)})

【讨论】:

  • 不需要在服务器端进行。某些事件的客户端,例如在您加载/更改该容器中的数据之后。将事件绑定到正文。
  • 但是这个问题被特别标记为node.js,你为什么认为它甚至被发送到浏览器
  • 你可以用cheerio = jQuery api implementation for node.js 很好的提示,谢谢!但我不知道'.some_div'
【解决方案6】:

在python中,这样做,但提供一个文件和标签的列表而不是硬编码的,然后包装在一个for循环中:

#!/usr/bin/env python
# encoding: utf-8
import re
f=open('fileWithHtml','r')

for line in f.readlines():
        line = re.sub('<p\s(.*)>[^<]', '<p>', line)
        print(line)

很可能,这可以很容易地转换为 nodejs 的 JavaScript

【讨论】:

  • 这个问题要求的是 node.js 脚本,而不是 python :-)
【解决方案7】:

您可以使用 DOM(或 SAX,取决于您想要做什么)解析器动态解析元素并删除所有遇到的样式属性。

在 JavaScript 上,你可以使用 HTML DOM removeAttribute() 方法。

<script>
  function myFunction()
  {
    document.getElementsByClassName("your div class")[0].removeAttribute("style"); 
};
</script>

【讨论】:

  • 你能提供简单的例子(JavaScript)吗?
【解决方案8】:

我正在提供客户端 (浏览器) 版本,因为当我在谷歌上搜索 删除 HTML 属性时出现了这个答案:

// grab the element you want to modify
var el = document.querySelector('p');

// get its attributes and cast to array, then loop through
Array.prototype.slice.call(el.attributes).forEach(function(attr) {

    // remove each attribute
    el.removeAttribute(attr.name);
});

作为一个函数:

function removeAttributes(el) {

    // get its attributes and cast to array, then loop through
    Array.prototype.slice.call(el.attributes).forEach(function(attr) {

        // remove each attribute
        el.removeAttribute(attr.name);
    });
}

【讨论】:

    【解决方案9】:
    $ = cheerio.load(htmlAsString);
    
    const result = $("*")
     // specify each attribute to remove, "*" as wildcard does not work
    .removeAttr("class")
    .removeAttr("itemprop")
    .html();
    // if you also wanted to remove the inner text for some reason, comment out the previous .html() and use
    //.text("")
    //.toString();
    
    console.log("result", result);
    

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      相关资源
      最近更新 更多