简体   繁体   English

如果元素存在,Jquery标记“insertAfter”如何只显示一次,不会一次又一次地重复?

[英]How Jquery tag “insertAfter” display only once if element exist no repeat again and again?

Please suggest to me an answer for my question: how Jquery tag "insertAfter" display only once if element exist no repeat again and again? 请向我建议我的问题的答案:如果元素存在不再重复,Jquery标记“insertAfter”如何只显示一次?

$(document).ready(function (){$(".ddd").keypress(function (e) {

 if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
    $("<span style='color:red'>insert only character</span>").insertAfter( $(this)).show().fadeOut("slow");   
     return false;
}
 });
 });

Try using jQuery's .length property on your selector to check if it exists. 尝试在选择器上使用jQuery的.length属性来检查它是否存在。 If it doesn't, insert it: 如果没有,请插入:

 $(document).ready(function() { $(".ddd").keypress(function(e) { if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { if (!$("#err").length) $("<span id= 'err' style='color:red'>insert only character</span>").insertAfter($(e.target)); $("#err").show().fadeOut("slow"); return false; } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="ddd"></input> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM