jQuery 兄弟姐妹和 $(this) 模糊和焦点

发布于 2024-09-09 06:19:13 字数 812 浏览 1 评论 0原文

我有一个带有标签和输入字段的表单。当单击某个字段时,我想使用 CSS 设置该字段的样式,并在标签下的 div 中显示有关输入要求的一些信息。

focus() 和 Blur() 事件添加了用于设置字段样式的类,但尝试显示/隐藏信息 div 会使用 $(this).siblings() 触发所有字段上的方法

$(".input-field").focus(function() { 
    $(this).addClass("input-field-focus");
    $(this).siblings(".label-info").show();
    return false; 
});
$(".input-field").blur(function() { 
    $(this).removeClass("input-field-focus");
    $(this).siblings(".label-info").hide();
    return false; 
});


<label for="login">
    User name:<br />
    <div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</div>
    <input type="text" name="login" class="input-field" value="<?php echo (isset($_POST['login'])) ? $_POST['login'] : ""; ?>">
</label>    

I have a form with labels and input fields. When clicking into a field I want to style the field with CSS and display some information about the input requirements in a div under the label.

The focus() and blur() events add the classes for styling the field just fine but trying to show/ hide the info divs triggers the methods on ALL fields using $(this).siblings()

$(".input-field").focus(function() { 
    $(this).addClass("input-field-focus");
    $(this).siblings(".label-info").show();
    return false; 
});
$(".input-field").blur(function() { 
    $(this).removeClass("input-field-focus");
    $(this).siblings(".label-info").hide();
    return false; 
});


<label for="login">
    User name:<br />
    <div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</div>
    <input type="text" name="login" class="input-field" value="<?php echo (isset($_POST['login'])) ? $_POST['login'] : ""; ?>">
</label>    

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

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

发布评论

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

评论(2

山田美奈子 2024-09-16 06:19:13

好的,现在我看到问题了。您的 HTML 标记是错误的。 label 中不能有 div 元素,请使用 span 代替:

<label for="login">User name:
    <br />
    <span class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</span>
    <br />
    <input type="text" name="login" class="input-field" value="">
</label>  

或者将整个块包装在自己的 div 中:

<div>
    <label for="login">User name:</label>
    <div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</siv>
    <input type="text" name="login" class="input-field" value="">
</div> 

否则浏览器将更正生成以下内容的标记:

<label for="login">
    User name:<br />
</label> 
<div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</div>
<input type="text" name="login" class="input-field" value="">

因此每个 label-info 和 input 元素都将处于同一级别,因此彼此是同级元素。

Ok, now I see the problem. Your HTML markup is wrong. You cannot have a div element inside label, use span instead:

<label for="login">User name:
    <br />
    <span class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</span>
    <br />
    <input type="text" name="login" class="input-field" value="">
</label>  

or wrap the whole block in an own div:

<div>
    <label for="login">User name:</label>
    <div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</siv>
    <input type="text" name="login" class="input-field" value="">
</div> 

Otherwise the browser will correct the markup producing this:

<label for="login">
    User name:<br />
</label> 
<div class="label-info">minimum 6 chararcters: letters, numbers and symbols - _ . @</div>
<input type="text" name="login" class="input-field" value="">

So every label-info and input element will be on the same level and therefore siblings to each other.

请帮我爱他 2024-09-16 06:19:13

编辑:

根据您更新的代码,您应该使用 .prev() 而不是 .siblings()

$(".input-field").focus(function() {
    $(this).addClass("input-field-focus");
    $(this).prev(".label-info").show();
    return false;
});
$(".input-field").blur(function() {
    $(this).removeClass("input-field-focus");
    $(this).prev(".label-info").hide();
    return false;
});

原始答案:

您的代码工作正常:http://jsfiddle.net/D9qnk/

我的猜测是您最初隐藏了 .label-info 使用 visibility: hide; 而不是 display:none;

如果是这种情况,请将 css 切换为使用 display:none

.label-info {
    display: none;
}​

或者,如果您想使用 visibility 属性,请使用 .css() 更改其值:

$(this).siblings(".label-info").css('visibility','visible');

...

$(this).siblings(".label-info").css('visibility','hidden');

EDIT:

Based on your updated code, you should be using .prev() instead of .siblings().

$(".input-field").focus(function() {
    $(this).addClass("input-field-focus");
    $(this).prev(".label-info").show();
    return false;
});
$(".input-field").blur(function() {
    $(this).removeClass("input-field-focus");
    $(this).prev(".label-info").hide();
    return false;
});

Original answer:

Your code works fine: http://jsfiddle.net/D9qnk/

My guess is that your are initially hiding the .label-info using visibility: hidden; instead of display:none;

If that's the case, switch your css to use display:none instead.

.label-info {
    display: none;
}​

Or if you want to use the visibility property, then change its value using .css():

$(this).siblings(".label-info").css('visibility','visible');

...

$(this).siblings(".label-info").css('visibility','hidden');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文