Javascript 如何从after()中删除元素
我有这个html元素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
<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();
}
});