Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 删除某些按钮后div中没有元素的文本_Jquery - Fatal编程技术网

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">&times;</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">&times;</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">&times;</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()查看我的答案->应该可以