【问题标题】:blur() and focus() not working on jQuery with replaceWith()blur() 和 focus() 不适用于带有 replaceWith() 的 jQuery
【发布时间】:2013-12-03 08:56:07
【问题描述】:

我正在使用 jQuery 制作一个表单,当您单击某个字段时,它会在一个

中显示有关在该字段中做什么的提示

标签。使用我现在拥有的代码,它将适用于第一个元素,但之后如果我尝试单击另一个元素,它将不会覆盖以前的更改。我尝试在 blur 上使用 empty() ,但这不会清除任何内容,而且我不知道这是否真的能解决问题。如果有人能给我任何关于这里有什么问题的见解,我将不胜感激!

#helpP 是我要在其中更改文本的段落。其他 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>");
     });
 });

我尝试与第一个链接,看看是否有任何不同,但没有。

【问题讨论】:

  • 您是否尝试过将其替换为 html() 而不是 replaceWith()?

标签: jquery forms focus blur replacewith


【解决方案1】:

不要使用replaceWith(),尝试使用html()like,

$("#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
});

来自 replaceWith() 文档,

说明: 将匹配元素集中的每个元素替换为 提供的新内容并返回之前的元素集 删除。

你的elements 将是replacedp elements

【讨论】:

  • 太棒了!谢谢!我不敢相信我忽略了这么简单的事情。
猜你喜欢
  • 2017-11-20
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多