使用jquery隐藏/显示输入和文本区域中的标签

发布于 2025-01-05 04:34:44 字数 1063 浏览 4 评论 0原文

我正在尝试让输入和文本区域标签隐藏/显示功能正常工作,但我的 Jquery 技能还不够好..:/

你可能已经看过这个一千次了,我已经阅读了有关它的页面,但仍然我一直无法弄清楚。

代码的第一部分工作正常,当元素获得焦点时,它将文本更改为较浅的颜色,并在您开始键入时消失。可能写得不是很好,但目前已经尽力了。

$("input, #email_text").focusin(function(){

$("label.input", this).css("color", "#f3f3f3");

});

$("input, #email_text").keypress(function(){

$("label.input", this).fadeOut("normal");

});

在这里,我希望脚本执行以下操作:如果元素失去焦点,我希望标签文本淡入,除非用户在字段中写了一些内容。我可能把 if/else 语句搞砸了,而且我还没有掌握 .val 函数。

$("input, #email_text").focusout(function() {

if($("#email_text").val() !="") {

$("label.input", this).fadeIn("normal").css("color", "#f3f3f3");

}} else { 

("label.input", this).hide();

});

谁能帮我解决这个问题吗?我有点放弃这个,也许应该从更基本的东西开始。

提前致谢!

问候,

Vegar Vallestad

编辑:

html 看起来像这样:

<div id="email_text">
<label class="input">Please leave a message..</label>
<textarea name="message" id="email_text"><?php echo "</tex" . "tarea>"; ?>
</div>

I am trying to get a input and textarea label hide/show function to work, but my Jquery skills are not good enough..:/

You probably seen this a thousand times, and I have read page up and down about it, but still I havent been able to figure it out.

The first part of the code works fine, it changes the text to a lighter color when the element gets focus, and disappears when you start to type. It is probably not very well written, but it is to the best of my abilitys right now.

$("input, #email_text").focusin(function(){

$("label.input", this).css("color", "#f3f3f3");

});

$("input, #email_text").keypress(function(){

$("label.input", this).fadeOut("normal");

});

Here I want the script to do the following: If the element loose focus, I want the labeltext to fade back in, exept if the user have written something in the field. I probably have the if/else statement screwed up, and I have not yet got a grip on the .val function.

$("input, #email_text").focusout(function() {

if($("#email_text").val() !="") {

$("label.input", this).fadeIn("normal").css("color", "#f3f3f3");

}} else { 

("label.input", this).hide();

});

Can anyone give me a hand with this? I`m kind of giving up on this, and should probably have started with something more basic.

Thanks in advance!

Regards,

Vegar Vallestad

Edit:

The html looks like this:

<div id="email_text">
<label class="input">Please leave a message..</label>
<textarea name="message" id="email_text"><?php echo "</tex" . "tarea>"; ?>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2025-01-12 04:34:44
$("input, #email_text").on({
    focus: function() {
        $("label.input", this).css("color", "#f3f3f3");
    },
    keyup: function() {
        $("label.input", this).fadeOut();
    },
    blur: function() {
        if($("#email_text").val() !="") {
            $("label.input", this).css("color", "#f3f3f3").fadeIn();
        } else { 
            $("label.input", this).hide();
        }
    }
});
$("input, #email_text").on({
    focus: function() {
        $("label.input", this).css("color", "#f3f3f3");
    },
    keyup: function() {
        $("label.input", this).fadeOut();
    },
    blur: function() {
        if($("#email_text").val() !="") {
            $("label.input", this).css("color", "#f3f3f3").fadeIn();
        } else { 
            $("label.input", this).hide();
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文