首页 > 解决方案 > jQuery 到 Vanilla JS,删除  

问题描述

我想将这段 jQuery 转换为 Vanilla JS,但我没有找到任何功能可以做到这一点

$('p').html(function(i, html) {
  return html.replace(/ /g, '')
})

标签: javascriptjquery

解决方案


您可以使用该document.querySelectorAll方法选择所有p标签。

document.querySelectorAll("p").forEach(function(el) {
    var html = el.innerHTML
    html = html.replace(/ /g, '')
    el.innerHTML = html
})

推荐阅读