Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 div中删除所有文本内容,但保留HTML标记和结构_Javascript_Jquery_Html_Dom - Fatal编程技术网

Javascript 从HTML div中删除所有文本内容,但保留HTML标记和结构

Javascript 从HTML div中删除所有文本内容,但保留HTML标记和结构,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有: 在这里 文本,我想 撤除 我想: <div> Here <a href="#"> is </a> <p> Text, that I want to </p> be removed </div> 删除所有文本但保留HTML结构的最简单方法是什么?使用jquery.empty()方法 再见 普通javascript,递归解决方案: <!--input--> <

我有:


在这里
文本,我想

撤除
我想:

<div>
    Here
    <a href="#"> is </a>
    <p> Text, that I want to </p>
    be removed
</div>

删除所有文本但保留HTML结构的最简单方法是什么?

使用jquery.empty()方法

再见
普通javascript,递归解决方案:

<!--input-->
<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

$( ".hello" ).empty();

<!--result-->
<div class="container">
  <div class="hello"></div>
  <div class="goodbye">Goodbye</div>
</div>

非常简单

您可以创建一个函数/插件,该函数/插件将通过顶级元素中的元素递归,删除找到的任何文本节点:

var thing = document.getElementById('thing');
removeAllText(thing);
function RemoveText(el){
   el.children().each(function(){
     var $this = $(this);
     if ($this.children().length > 0){
       $this.children().each(RemoveText($this));
     }
     else{
       $this.text("");
     }     
   });
}

参考资料:


显然,您希望从元素中删除所有文本节点。您可以使用函数访问文本节点。您不需要任何递归。jQuery为您完成了以下任务:

$(函数(){
$(“#要清理,#要清理*”)//选择元素及其内部的所有元素节点
.contents()//选择所有子节点,包括标记、注释和文本
.filter(函数(){
返回this.nodeType===Node.TEXT\u Node;//筛选文本节点
}).remove();//动臂!
});

在这里
文本,我想

撤除
试试:

函数为空(e){
var childs=e.childs;

对于(var i=0;i还有一个方法,只是为了好玩和学习。尝试在不使用本机JS函数、检查元素属性的情况下执行此操作……令人惊讶的是,它可以工作

function empty(e) {
    var childs = e.children;
    for(var i=0;i<childs.length;i++) {
        if(childs[i].children.length!=0) {
            childs[i].innerHTML=" ";
        } else {
            empty(childs[i]);
        }
    }
}
clones=[];
$('div').children().each(function()){
clone.push($(this.clone().text(“”));
});
$('div').empty();

对于(i=0;iyou可以使用:
.empty()
它还将删除子元素。使用
.text(“”
而不是
empty()
.empty()用于删除元素,而不是文本内容。将进行编辑,并检查是否有子元素来进行递归我不确定为什么会被否决…这是最好的答案不知道是谁和为什么否决了它。但是,这并不能解决整个问题。您需要删除此处的世界
被删除
以及:@DontVoteMeDown然后他可以迭代childNodes?这是一个干净的,普通的js解决方案。我的意思是,他可以添加ID,OP的代码根本没有ID。他就像..不迭代..IE9+,以及Chrome,FF,Safari和Opera的每一个版本。是的,非常好。可能是最简单和干净的方法!几分钟后更正一个错误答案:)
function RemoveText(el){
   el.children().each(function(){
     var $this = $(this);
     if ($this.children().length > 0){
       $this.children().each(RemoveText($this));
     }
     else{
       $this.text("");
     }     
   });
}
$.fn.removeText = function(){
  this.each(function(){

     // Get elements contents
     var $cont = $(this).contents();

      // Loop through the contents
      $cont.each(function(){
         var $this = $(this);

          // If it's a text node
          if(this.nodeType == 3){
            $this.remove(); // Remove it 
          } else if(this.nodeType == 1){ // If its an element node
            $this.removeText(); //Recurse
          }
      });
  });
}

$('#toplevel').removeText();
function empty(e) {
    var childs = e.children;
    for(var i=0;i<childs.length;i++) {
        if(childs[i].children.length!=0) {
            childs[i].innerHTML=" ";
        } else {
            empty(childs[i]);
        }
    }
}
clones=[];

$('div').children().each(function() {

clones.push($(this).clone().text(''));
});

$('div').empty();

for(i=0;i<clones.length;i++) {

 clones[i].appendTo('div');
}