单击html中的输入时的工具提示问题

时间:2011-08-23 16:02:51

标签: jquery html css

我目前正在开发一个HTML,CSS,PHP项目。我想显示一个工具提示,以便在用户单击表单上的输入字段时向用户提供一个小解释。我希望工具提示出现在输入字段旁边,但略微覆盖表单,但是当它出现时,它会环绕表单的边缘而不是继续。我已经尝试将工具提示的z-index更改为1,但它没有任何区别。使用HTML,CSS和JQuery制作了tooptip。下面是我用来执行此操作的代码。

以下是表格的代码

<form action="index.php" method="post" style="margin-top: auto; margin-bottom: auto;">
    <table class="formTable">
         <tr><td>
    <label for="txtUsername">Username: </label>
    </td>
    <td>
           <input class="forms" type="text" name="txtUsername" placeholder="Username" required autofocus />
           <span class="tooltip" style="z-index: 1;">Please enter your username</span>
        </td>
        </tr>
        <tr>                    
            <td>
            <label for="txtPassword">Password: </label>
            </td>
            <td>
            <input class="forms" type="password" name="txtPassword" placeholder="Password" required /><br />
            </td>
            </tr>
            <tr><td colspan="2">
                <div class="buttonGroup">
                <input class="button" type="submit" name="btnSubmit" text="Submit" /> <input class="button" type="reset" text="Reset" />        </div>
            </td>
            </tr>
        </table>
       </form>

以下是表单的CSS代码

form
{
    width: 500px;
    text-align: center;
    height: auto;
    background-color: #95b0ff;
    margin-left: auto;
    margin-right: auto;
    padding: 5px;
    border-width: 1px;
    border-color: black;
    border-style: solid;
    -webkit-border-radius:5px;
    -moz-border-radius:5px;
    border-radius:5px;
    z-index: 1;

}

以下是工具提示的CSS

.tooltip {
    padding: 5px;
    margin-left: 50px;
    background: #666;
    /*border: 1px solid #606060;*/
    border: 1px solid white;
    color: #ddd;
    -border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    box-shadow: 0 1px 3px #111;
    -moz-box-shadow: 3px 3px 1px #999;
    -webkit-box-shadow: 3px 3px 1px #999;
    z-index: 2;
 }

以下是显示工具提示的JQuery

$(function() {
    $('.tooltip').hide();
    $('.tooltip').prev('input').focus(function(){
        $(this).next().fadeIn('slow');
    }).focusout(function(){
        $(this).next().fadeOut('slow');
    });
});

感谢您提供的任何帮助。

更新 感谢@Rikudo Sennin,这有点奏效。我现在唯一的问题是工具提示不会出现在输入字段旁边,而是会显示在下方,如下图所示

Form showing tooltip not aligned with input

2 个答案:

答案 0 :(得分:1)

不确定这是你在找什么:

.tooltip {
     position:absolute;
}

答案 1 :(得分:0)

在父级上使用position: relative,在工具提示上使用position: absolute

<强> Working Example

此外,在不相关的注释中,for=的{​​{1}}属性搜索<label> s匹配<input> s而非id= s。