javascript - 焦点仅显示一个元素

标签 javascript jquery focus this

我有一个简单的问题:

我有一个表格:

<form>
    <textarea type="text" name="comment_text" class="comment-input"></textarea>
    <div class="remaining"></div>
    <input type="submit" class="button comment-button" />
</form>

现在,当textarea(.comment-text)聚焦时,我希望使用 jQuery 显示提交按钮(.comment-button).

$(document).ready(function() {
    //display the comment button when the comment textarea is focused
    $('.comment-input').focus(function() {
        $('.comment-button').fadeIn(800);
    });
});

这很好用。 问题是我的表单位于foreach循环中,当我聚焦一个文本区域时,所有按钮都会被选中。所以我尝试使用“this”关键字来做到这一点,如下所示:

$(document).ready(function() {
       //display the comment button when the comment textarea is focused
        $('.comment-input').focus(function() {
            $(this).find('.comment-button').fadeIn(800);
        });
    });

但这不起作用。在尝试了太长时间之后,我决定我不会说足够的 jQuery 来完成这个简单的任务,并向知情人士寻求帮助!
提前谢谢!

最佳答案

这是因为按钮是文本区域的同级元素

$(this).siblings('.comment-button').fadeIn(800);

关于javascript - 焦点仅显示一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880678/

相关文章:

javascript - 使用 angularjs 应用程序进行 Bootstrap 的数据表

ios - iOS上的Phonegap应用程序-页面在文本输入焦点上滚动

javascript - CSS 或 JS - 当输入聚焦时更改输入的背景颜色?

javascript - 循环遍历 Angular 对象时向 HTML 添加 id

javascript - 有没有更好的方法来根据路线显示内容?

javascript - 使用 Jquery 的条件日期

javascript - jQuery - 每当用户更改任何表单输入时调用一个函数?

jquery - 所有文本字段中的值计算时出现问题

java - 获取 ID,Selenium 中当前关注的元素的类

javascript - 事件触发后聚焦于某个元素