javascript - jQuery 在悬停时显示/隐藏特定 <li></li> 中的 div

标签 javascript jquery html

我有一个显示列表的页面,用户可以向其中添加和删除项目。

在每个<li></li>上,有一个与该项目匹配的小删除按钮。

目前,删除按钮仅在用户将鼠标悬停在列表上时显示。 我想做的是当用户将鼠标悬停在特定项目上时一次仅显示一个删除按钮。

这是我到目前为止所拥有的:

$(document).ready(function() {
    $(".delete_update").hide(); 
    $('.cell').hover(function(){
        $(".delete_update").show();

        $('.cell').mouseout(function(){
            $(".delete_update").hide();
        });
    });
});


<li class="cell" id="post<?php echo $postid ?>">
    <div id="update<?php echo $postid ?>">
        <?php echo $post ?>
    </div>
    <div id="eraser<?php echo $postid ?>">
        <a href="#" id="<?php echo $postid ?>" class="delete_update">Delete !</a>
    </div>
</li>

我尝试向 jQuery 添加一个变量来包含每个单元格的“id”,例如:

var element = $(this);
var I = element.attr("id");
$('.cell' + I).hover(function() {
    $(".delete_update").show();
});

但这行不通。

有什么想法吗?

最佳答案

试试这个:

$(function(){
    $('.cell').hover(function(){ //Hover takes 2 callbacks one for mouseenter and one for mouseleave
          $(this).find('.delete_update').show(); //show the button which is inside the current li hovered
    },
     function(){
         $(this).find('.delete_update').hide(); //hide the button which is inside the current li hovered

     });
});

或者只使用切换

 $(function(){
        $('.cell').hover(function(){ // Same callback will be executed if only one is mentioned,  on mouseeneter and mouse leave
              $(this).find('.delete_update').toggle(); //toggle the button visibility which is inside the current li hovered
        }
    });

关于javascript - jQuery 在悬停时显示/隐藏特定 <li></li> 中的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17182655/

相关文章:

html - 将表格宽度设置为 100% 不会填充区域

javascript - 轮播 slider 加载 ajax 后自动播放

jquery - Cheerio jquery Node js : obtaining href value

html - 使用 CSS 居中对齐

jquery - 将类添加到动态创建的元素时出现问题

javascript - jQuery 在粘贴之前重写复制的文本

javascript - 如何用 pie div 元素创建一个圆圈?

javascript - 如何有条件地隐藏 native 脚本( Angular )中的选项卡项目?

javascript - 我必须在 Javascript 中转义正斜杠吗?

javascript - 将 httpClient 答案转换为模型对象 [Angular 6]