2

我正在尝试使用 jquery 来选择下面 HTML 文档中的所有输入,并在“keydown”事件中选择“this”,然后显示下一个元素。

我的 jQuery

$(document).ready(function(){
    $('.hint').hide();
});
    $('input').keydown(function(){
        $(this).show();
});

我的 HTML

<!DOCTYPE html>
<html>
<head>
<title>Title of my Page</title>
</head>
<body>

<p>
    <input type="text"><span class="hint">Something cool</span>
</p>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
4

4 回答 4

4

目前尚不清楚您在问什么,但您的第二个函数在页面准备好后没有运行,因此请注意在脚本之前加载 DOM。

$(document).ready(function(){

    $('.hint').hide();

    $('input').keydown(function(){
        $(this).show();
    });

});

要显示下一个元素,请将行更改为:

 $(this).next().show();
于 2012-12-27T22:54:10.223 回答
0

也许您可以尝试在 jQuery 中使用 .next( [selector] ) 函数。(我假设你想显示 .hint 跨度)

$(document).ready(function() {
    $('.hint').hide();
});
$('input').keydown(function() {
    $(this).next('.hint').show();
});​

这是一个示例:http: //jsfiddle.net/RQ3zw/

于 2012-12-27T22:56:30.070 回答
0

啊! 找到了!

只需要添加

 $(this).next().show();

代替

$(this).show();

所以完成这将是:

$(document).ready(function(){
    $('.hint').hide();
});
    $('input').keydown(function(){
        $(this).next().show();
});
于 2012-12-27T22:56:54.580 回答
0

使用.next()选择器:

$(this).next('.hint').show();

在这里拉小提琴。

于 2012-12-27T22:58:33.287 回答