jquery删除节点并保留子节点

时间:2016-11-10 11:11:49

标签: jquery html children parents

发布这个问题我感到非常惭愧但是我还没有设法在Jquery中使用unwrap或replaceWith方法来满足这个需求。

我的问题很简单:我需要删除一些html代码的节点(jquery选择器),而不会丢失这些节点的子节点。

这是一个Jsfiddle,它展示了用于实现我的目标{* 3}}的难看的代码的结果(是的,它的作品......)

// var content : the html source code of the wysiwyg

var result = '';
$(content).contents().each(function(){
    var addContent = '';
    // textNode
    if(this.nodeType == 3) {
        // Text Node
        result+= $(this).text();
    } else if(this.nodeType == 1 && $(this).hasClass('atwho-inserted')) {
        // if is an Object Node with the target class
        // I only keep it's contents (means that ".atwho-inserted" is not kept)
        result+= $(this).html();
    } else {
        // in any other case I keep it entirely
        result+= this.outerHTML;
    }
});

你能找到一个更好的代码(使用unwrap方法)吗?

非常感谢你:)

4 个答案:

答案 0 :(得分:1)

如果您的目的是仅删除没有子女的span.atwho-inserted且剩余的DOM保持不变,您可以通过以下方式删除:

$('.start .atwho-inserted').children(':first-child').unwrap();

首先,选择类.atwho-inserted的元素,然后找到他们各自的第一个孩子并执行unwrap

unwrap删除选择器直接父包装器并留下子进程。

你很高兴去!

答案 1 :(得分:1)

我觉得这个会让你高兴的。 注意修改var tmp



$(document).ready(function(){
  var content = $('.start').html();
  $('.startCode code').text(content);
  
  var tmp = $(content);
  $('.atwho-inserted', tmp).children().unwrap();
  var result = tmp[0].outerHTML; // or tmp.html(); but would lose the outermost tag
  
  $('.result').html(result);
  $('.resultCode').text(result);
});

body{
  padding:10px;
}
.atwho-inserted{
  background-color:red;
}
pre code, .wrap{
  white-space: pre-line;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="container">
  <div class="row">
    <div class="col-xs-6">
      <div class="well">All selectors ".atwho-inserted" are in "background-color:red" and have to be removed without losing its children.</div>
      <hr/>
      <div class="start">
        <p>
          <span class="atwho-inserted" data-atwho-at-query="@fac" contenteditable="false">
            <span class="user_mention" data-identifier="8930">@facticeuserr</span>
          </span>
          <br /> some lorem ipsum text
          <span class="atwho-inserted" data-atwho-at-query="#acc" contenteditable="false">
            <span class="tag_mention" data-identifier="484">#accessibilité</span>
          </span>
          <br>
          <img src="http://lorempixel.com/100/100/" data-filename="3">
          <br />
          <b>hello</b>
          <br>
          <span class="atwho-inserted" data-atwho-at-query="#acc" contenteditable="false">
            <span class="tag_mention" data-identifier="653">#Accompagnement</span>
          </span>
        </p>
      </div>
      <pre class="startCode"><code></code></pre>
    </div>
    <hr/>
    <div class="col-xs-6">    
      <div class="well">Here the result<br /> it's works but code is not optimized</div>
      <hr/>
      <div class="result"></div>
      <pre class="resultCode"><code></code></pre>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

基本上outerHTML是您要用innerHTML替换的内容( unwrap ) 检查https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML#Browser_compatibility

$('#trigger').click(function() {
  $(this).hide();
  $('.atwho-inserted').each(function() {
    this.outerHTML = $(this).html();
  });
});
body{
  padding:10px;
}

.atwho-inserted{
  background-color:red;
}

pre code, .wrap{
  white-space: pre-line;
}

#trigger {
  font-size: 1.5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="trigger">Click to trigger edition</button>

<div class="container">
  <div class="row">
    <div class="col-xs-6">
      <div class="well">All selectors ".atwho-inserted" are in "background-color:red" and have to be removed without losing its children.</div>
      <div class="start">
        <p>
          <span class="atwho-inserted" data-atwho-at-query="@fac" contenteditable="false">
            <span class="user_mention" data-identifier="8930">@facticeuserr</span>
          </span>
          <br /> some lorem ipsum text
          <span class="atwho-inserted" data-atwho-at-query="#acc" contenteditable="false">
            <span class="tag_mention" data-identifier="484">#accessibilité</span>
          </span>
          <br>
          <img src="http://lorempixel.com/100/100/" data-filename="3">
          <br />
          <b>hello</b>
          <br>
          <span class="atwho-inserted" data-atwho-at-query="#acc" contenteditable="false">
            <span class="tag_mention" data-identifier="653">#Accompagnement</span>
          </span>
        </p>
      </div>
      <pre class="startCode"><code></code></pre>
    </div>
    <div class="col-xs-6">    
      <div class="well">Here the result<br /> it's works but code is not optimized</div>
      <div class="result"></div>
      <pre class="resultCode"><code></code></pre>
    </div>
  </div>
</div>

答案 3 :(得分:0)

这是另一个JsFiddle,我添加了unwrap()解决方案 https://jsfiddle.net/uddaeh1u/21/

$(content).find('.atwho-inserted').children(':first-child').unwrap();

我认为问题在于unwrap()只返回解包的jquery对象,并且只能应用于DOM(而不是来自被视为Dom对象的变量)。