Jquery 删除某些按钮后div中没有元素的文本
这是密码Jquery 删除某些按钮后div中没有元素的文本,jquery,Jquery,这是密码 <div id="user-alert" class="alert alert-danger col-md-offset-1 col-md-10 alert-dismissible" role="alert" > <button type="button" class="close" data-hide="alert" aria-hidden="true"> <span aria-hidden="true"&
<div
id="user-alert"
class="alert alert-danger col-md-offset-1 col-md-10 alert-dismissible"
role="alert"
>
<button type="button" class="close" data-hide="alert" aria-hidden="true">
<span aria-hidden="true">×</span>
</button>
myquestion is,, how to remove/delete this text using jquery, just this text only,,
</div>
&时代;
我的问题是,如何使用jquery删除/删除此文本,仅此文本,,
我尝试了$('#用户警报:不是(:第一个孩子)).remove()代码>但它不起作用
我还尝试了$(“#用户警报按钮.关闭)。文本(“”)
仍然不起作用元素最后一个节点的选择器是$(“#用户警报”).contents().last()[0]
,使用该选择器可以删除它的文本
$('#user-alert').contents().last()[0].textContent='';
工作片段:
$(“#用户警报”).contents().last()[0]。textContent=''代码>
&时代;
我的问题是,如何使用jquery删除/删除此文本,仅此文本,,
试试这个
已更新
$( "#YourIdHere" ).contents().filter(function() {
return this.nodeType === 3;
}).remove();
如果我理解正确,试着把你的文本放在一个空格里。例如:
<div
id="user-alert"
class="alert alert-danger col-md-offset-1 col-md-10 alert-dismissible"
role="alert">
<button type="button" class="close" data-hide="alert" aria-hidden="true">
<span aria-hidden="true">×</span>
</button>
<span id="myText">myquestion is,, how to remove/delete this text using jquery, just this text only,,</span>
</div>
<script type="text/javascript">
$('#myText').remove();
</script>
&时代;
我的问题是,如何使用jquery删除/删除此文本,仅此文本,,
$(“#myText”).remove();
您可以保留对按钮的引用,并在清除html后将其添加回div
var myDiv = jQuery('#user-alert');
var myButton = jQuery('button');
myButton.click(function(e) {
e.preventDefault();
myDiv.html('');
myButton.appendTo(myDiv);
});
我想这对你有帮助
<div
id="user-alert"
class="alert alert-danger col-md-offset-1 col-md-10 alert-dismissible"
role="alert"
>
<button type="button" class="close" data-hide="alert" aria-hidden="true">
<span aria-hidden="true">×</span>
</button>
myquestion is,, how to remove/delete this text using jquery, just this text only,,
</div>
<script type="text/javascript">
$(function(){
$('.close').on('click',function(){
$(this).closest('div').remove();
})
})
</script>
&时代;
我的问题是,如何使用jquery删除/删除此文本,仅此文本,,
$(函数(){
$('.close')。在('click',function()上{
$(this).closest('div').remove();
})
})
您可以使用closest()
或parents()
或者$($('#用户警报')[0].childNodes.last().remove()代码>查看我的答案->应该可以