JQUERY:如何在DIV中删除除BR和IMG之外的所有HTML标签 [英] JQUERY: How to remove all HTML tags except BR and IMG in DIV
本文介绍了JQUERY:如何在DIV中删除除BR和IMG之外的所有HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在里面书写时,如何删除DIV Contenteditable中除<img>
和<br>
之外的所有HTML标记
How do I delete all HTML tags except <img>
and <br>
in DIV Contenteditable, when I'm writing inside
<div id='aa' contenteditable='true'></div>
到目前为止,我正在使用
So far i am using
$('#aa').text()
但是它会删除所有内容
我尝试使用此
$('#aa').keydown(function(e) {
var datasts=$("#aa").html();
datasts=datasts.substr(datasts.length - 1);
if (e.keyCode === 13) {
document.execCommand('insertHTML', false, 'enter');
return false;
}
});
document.getElementById("aa").addEventListener("input", function() {
var a=$("#aa").text();
$("#aa").html(a.replace('enter', '<br/>'));
}, false);
但是结果不是最大的
这是我更改的代码
<div id='aa' contenteditable='true'></div>
<button onclick="$('#aa').html('<img src='https://ichef.bbci.co.uk/news/976/media/images/83351000/jpg/_83351965_explorer273lincolnshirewoldssouthpicturebynicholassilkstone.jpg'>');">
aa
</button>
<script>
$('#aa').keydown(function(e) {
var datasts=$("#aa").html();
datasts=datasts.substr(datasts.length - 1);
if (e.keyCode === 13) {
document.execCommand('insertHTML', false, '<br>');
return false;
}
});
document.getElementById("aa").addEventListener("input", function() {
$("#aa").children().not("br, img").remove();
}, false);
</script>
我想删除处于书写位置的html标签.复制html时,html标记将自动删除
推荐答案
尝试一下,如果#aa的子代不是br或img,它将删除该子代.
try this, that will remove any child of #aa if that child isn't a br or an img
$("#aa").children().not("br, img").remove()
这篇关于JQUERY:如何在DIV中删除除BR和IMG之外的所有HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文