这里是代码
<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>
我已经尝试了$('#user-alert :not(:first-child)').remove();
,但它无效
我也试过$('#user-alert button.close).text('')
仍无法正常工作
答案 0 :(得分:1)
元素的最后一个节点的选择器是$('#user-alert').contents().last()[0]
,使用该选择器可以删除它的文本。
$('#user-alert').contents().last()[0].textContent='';
工作代码段
$('#user-alert').contents().last()[0].textContent='';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
答案 1 :(得分:1)
试试这个 <强>已更新强>
$( "#YourIdHere" ).contents().filter(function() {
return this.nodeType === 3;
}).remove();
答案 2 :(得分:0)
如果我理解正确,请尝试将文字放在一个范围内。例如:
<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>
答案 3 :(得分:0)
您可以保留对该按钮的引用,并在清除html后将其添加回div。
var myDiv = jQuery('#user-alert');
var myButton = jQuery('button');
myButton.click(function(e) {
e.preventDefault();
myDiv.html('');
myButton.appendTo(myDiv);
});
答案 4 :(得分:0)
我认为这会对你有所帮助
<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>
您可以使用closest()
或parents()