简体   繁体   English

blur()和focus()在带有replaceWith()的jQuery上不起作用

[英]blur() and focus() not working on jQuery with replaceWith()

I'm working on a form with jQuery that when you click on a certain field, it will display tips on what to do in that field in a 我使用的是jQuery表单,当您单击某个字段时,它将显示有关在该字段中做什么的提示。

tag. 标签。 With the code I have now, it will work for the first element, but then after that if I try to click another one it will not overwrite the previous change. 使用我现在拥有的代码,它将对第一个元素起作用,但是之后,如果我尝试单击另一个,它将不会覆盖以前的更改。 I tried to use empty() on blur but that won't clear anything out, and I don't know if that would really solve the problem anyway. 我尝试在模糊时使用empty(),但无法清除任何内容,而且我也不知道这是否真的可以解决问题。 If anybody could give me any insight with what's wrong here I would really appreciate it! 如果有人对我的问题有任何见解,我将不胜感激!

#helpP is the paragraph that I want to change text within. #helpP是我要在其中更改文本的段落。 The other IDs are just form fields. 其他ID只是表单字段。

$(document).ready(function () {
    $("#mail").focus(function() {
        $("#helpP").replaceWith("<p>This e-mail address will be used as your login and for receiving special offers from Volga</p>");
    }).blur(function(){
        $("helpP").empty();
    });

    $("#confirmEmail").focus(function() {
        $("#helpP").replaceWith("<p>Confirm your e-mail</p>");
    });

    $("#confirmEmail").blur(function() {
        $("#helpP").empty();
    });

    $("#passwordReg").focus(function() {
        $("#helpP").replaceWith("<p>Passwords can only have letters and numbers and are case sensitive.</p>");
    });

     $("#passwordRegConfirm").focus(function() {
         $("#helpP").replaceWith("<p>Enter the same password again to ensure accuracy</p>");
     });
 });

I tried chaining with the first one to see if that made any difference, but it does not. 我尝试与第一个链接,以查看是否有任何区别,但是没有。

Don't use replaceWith() try to use html() like, 不要使用replaceWith()尝试使用html()之类的方法,

$("#mail").focus(function() {
    $("#helpP").html("<p>This e-mail address will be used as your login and for receiving special offers from Volga</p>");
}).blur(function(){
    $("#helpP").empty();// use # before id selector
});

From replaceWith() Docs, replaceWith()文档中,

Description: Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. 说明:用提供的新内容替换匹配元素集中的每个元素,并返回被删除的元素集。

And it your elements will be replaced with p elements 而且您的elements将被p elements replaced

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

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