Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 清除html内容_Javascript_Jquery_Html - Fatal编程技术网

Javascript 清除html内容

Javascript 清除html内容,javascript,jquery,html,Javascript,Jquery,Html,我有一个用户提交的包含HTML的数据,我正在将该数据存储在数据库中。 数据,例如: 一个 第二 第三 问题并不是很清楚您到底想清理什么,但有一个jQuery函数: $(".myclass").html(""); 这将把所选元素中的html替换为空 $( document ).ready(function() { $('p').each(function() { var $this = $(this); if($this.html().replace(/\s|&

我有一个用户提交的包含HTML的数据,我正在将该数据存储在数据库中。 数据,例如:

一个

第二

第三


问题并不是很清楚您到底想清理什么,但有一个jQuery函数:

$(".myclass").html("");
这将把所选元素中的html替换为空

$( document ).ready(function() {
    $('p').each(function() {
    var $this = $(this);
    if($this.html().replace(/\s| /g, '').length == 0)
        $this.remove();
    });
});
此jQuery代码删除为空

用法:空选择器 如果你有大问题,我不确定这是否是一个好的解决方案 $*:empty.remove 一个

第二

第三


您可以使用js清理父级


OP也需要的空div可能重复吗?$'div.eachfunction{var$this=$this;if$this.html.replace/\s |/g,.length==0$this.remove;};尝试将“p”更改为“div”或查找class=我收到的html数据包含空标记,例如class和其他没有内容的标记,所以我想删除它们并将干净的html存储在数据库中
function clean(parent){
    var children=parent.children
    var i=0
    while(i<children.length){
      if(children[i].innerHTML==""){
        parent.removeChild(children[i])
        i--;
      }
      i++;
    }
}

var parent=document.getElementsByClassName('myclass')[0]
clean(parent)