Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
Javascript 如何从after()中删除元素_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何从after()中删除元素

Javascript 如何从after()中删除元素,javascript,jquery,html,Javascript,Jquery,Html,我有这个html元素 <input type='text' class='req-in' name='fname'> <input type='text' class='req-in' name='lname'> 我有这个剧本 $('.req-in').blur(function() { if($(this).val() == ''){ $(this).after('<p class="display">some text here

我有这个html元素

<input type='text' class='req-in' name='fname'>
<input type='text' class='req-in' name='lname'>

我有这个剧本

$('.req-in').blur(function() {
    if($(this).val() == ''){
       $(this).after('<p class="display">some text here for this field only!</p>');   
    }else{
       // I'm trying to remove the "p.display" for this only!
    }
});
$('.req in').blur(函数(){
if($(this.val()=''){
$(this).after(“

此处的某些文本仅用于此字段!

”); }否则{ //我正试图删除“p.display”仅用于此目的! } });

我试图
$('.display').remove()
但是所有的
p.display
都被删除了,我如何处理这个问题,因为只有
p.display
的字段才会被删除?

您可以使用
.next()
方法:

$(this).next('.display').remove();
在重新追加消息之前,您还可以检查下一个同级是否存在:

var message = '<p class="display">some text here for this field only!</p>';

$('.req-in').blur(function() {
    var $this = $(this);
    if ( $.trim(this.value) === '' ) {
       // if the next '.display' sibling doesn't exist
       if ( $this.next('.display').length === 0 ) {
          $this.after(message);
       }
    } else {
       $this.next('.display').remove();
    }
}); 
var message='

此处的一些文本仅用于此字段

",; $('.req in').blur(函数(){ var$this=$(this); 如果($.trim(此.value)=''){ //如果下一个“.display”同级不存在 if($this.next('.display')。长度==0){ $this.after(消息); } }否则{ $this.next('.display').remove(); } });