将h2标签更改为h1标签

时间:2019-10-10 13:50:59

标签: javascript html

我想将所有产品页面上的产品名称从h2更改为h1

我想通过自定义代码将此方式更改为h1。

<h2 itemprop="name" class="product_title likeh2">Aspen Ágykeret Bársony Sötétzöld 160 cm</h2>

2 个答案:

答案 0 :(得分:0)

您可以这样:

//select all h2 elements
document.querySelectorAll('h2').forEach( element => {
  //create new h1 element
  const newElement = document.createElement('h1');

  //set the inner html to the original h2 element 
  newElement.innerHTML = element.innerHTML;

  //take all attributes from original element and assign them to the new one
  Array.from(element.attributes).forEach( attr => {
    newElement.setAttribute(attr.nodeName, attr.nodeValue)
  })

  //replace the node in the dom
  element.parentNode.replaceChild(newElement, element);
})

答案 1 :(得分:-1)

这是一种方法。这将取决于浏览器对externalHTML的支持,但如今看起来不错:https://nodejs.org/en/

使用

dbutils.fs.mount( source = "wasbs://<container-name>@<storage-account-name>.file.core.windows.net", mount_point = "/mnt/<mount-name>", extra_configs = {"<conf-key>":dbutils.secrets.get(scope = "<scope-name>", key = "<key-name>")}) 而不是querySelectorAll是因为元素的数组即使元素更改也不会更改。当您实际更新该数组中的元素时,它会使用引用来更新实际元素,但在数组中不会更改。这样,我们就可以继续循环浏览和更改文档上的所有getElementsByTagName标签,而每次<h2>标签转换为{{1时,getElementsByTagName返回的活动列表将丢失元素}}。

h2
h1

相关问题