jquery 删除节点并保留子节点
jquery remove nodes and keep children
我很惭愧 post 这个问题,但我还没有设法在 Jquery 中使用 unwrap 或 replaceWith 方法来满足这个需要。
我的问题很简单:我需要删除 html 代码的一些节点(jquery 选择器)而不丢失这些节点的子节点。
这是一个 Jsfiddle,它展示了我用于实现目标的难看代码的结果 https://jsfiddle.net/uddaeh1u/15/(是的,它有效...)
// 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 方法)吗?
非常感谢:)
基本上 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>
如果您的目的是仅删除 span.atwho-inserted
而没有其 children 并且 DOM 的其余部分保持不变,您可以通过以下方式实现:
$('.start .atwho-inserted').children(':first-child').unwrap();
首先selectclass.atwho-inserted
的元素,然后找到它们各自的first-child并执行unwrap
。
unwrap
删除 select 或直接 parent 包装器并留下 children.
一切顺利!
我想这个会让你满意。
注意修改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>
这是另一个 JsFiddle,我在其中添加了 unwrap() 解决方案
https://jsfiddle.net/uddaeh1u/21/
$(content).find('.atwho-inserted').children(':first-child').unwrap();
我认为问题在于 unwrap() 仅 returns jquery 对象解包并且只能应用于 DOM (不是来自被视为 Dom 对象).
我很惭愧 post 这个问题,但我还没有设法在 Jquery 中使用 unwrap 或 replaceWith 方法来满足这个需要。
我的问题很简单:我需要删除 html 代码的一些节点(jquery 选择器)而不丢失这些节点的子节点。
这是一个 Jsfiddle,它展示了我用于实现目标的难看代码的结果 https://jsfiddle.net/uddaeh1u/15/(是的,它有效...)
// 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 方法)吗?
非常感谢:)
基本上 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>
如果您的目的是仅删除 span.atwho-inserted
而没有其 children 并且 DOM 的其余部分保持不变,您可以通过以下方式实现:
$('.start .atwho-inserted').children(':first-child').unwrap();
首先selectclass.atwho-inserted
的元素,然后找到它们各自的first-child并执行unwrap
。
unwrap
删除 select 或直接 parent 包装器并留下 children.
一切顺利!
我想这个会让你满意。
注意修改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>
这是另一个 JsFiddle,我在其中添加了 unwrap() 解决方案 https://jsfiddle.net/uddaeh1u/21/
$(content).find('.atwho-inserted').children(':first-child').unwrap();
我认为问题在于 unwrap() 仅 returns jquery 对象解包并且只能应用于 DOM (不是来自被视为 Dom 对象).