javascript - jQuery - 鼠标离开
问题描述
我在下面有一个非常简单的代码。当我悬停其中一个红色小方块时,会出现另一个大颜色方块。
问题:当我将光标移开这个大方块时,这个方块会被 隐藏mouseleave().hide()
,但它不起作用。
请帮忙。
HTML
<table class="table" style="width:100%">
<tr>
<td>
<div class="hot-spot" data-target="black"></div>
<div ID="black"></div>
</td>
<td>
<div class="hot-spot" data-target="green"></div>
<div ID="green"></div>
</td>
<td>
<div class="hot-spot" data-target="blue"></div>
<div ID="blue"></div>
</td>
<td>
<div class="hot-spot" data-target="yellow"></div>
<div ID="yellow"></div>
</td>
</tr>
</table>
JS
$(function() {
$('.hot-spot').hover(function (e) {
var square = $(this).data('target');
$('#' + square).show();
$('#' + square).mouseleave.hide();
});
});
解决方案
您只需要在 mouseleave 之后添加括号以显示它是一个函数:
$(function() {
$('.hot-spot').hover(function(e) {
var square = $(this).data('target');
$('#' + square).show();
$('#' + square).mouseleave(function() {
$('#' + square).hide();
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" style="width:100%">
<tr>
<td>
<div class="hot-spot" data-target="black">a</div>
<div ID="black">black</div>
</td>
<td>
<div class="hot-spot" data-target="green">b</div>
<div ID="green">green</div>
</td>
<td>
<div class="hot-spot" data-target="blue">c</div>
<div ID="blue">blue</div>
</td>
<td>
<div class="hot-spot" data-target="yellow">d</div>
<div ID="yellow">yellow</div>
</td>
</tr>
</table>
推荐阅读
- kotlin - 如果最后一个操作以异常结束,Kotlin 协程立即给出异常
- ajax - 使用随机会话号保护 web api
- ubuntu-16.04 - Skylake 硬件亮度变化过于精细
- mysql - MySql 下拉过滤器选择 NULL
- git - Merge a remote branch on Heroku to Heroku Master to trigger a build
- javascript - Javascript - 跳过语句
- c# - 一旦发生重新调整大小,C# WPF 就会删除画布宽度和高度
- java - 无法在spring mvc中使用get请求调用控制器方法
- javascript - Javascript 显示“无法设置属性‘innerHTML’为空”
- php - $wpdb->prepare 在 WordPress 后端 PHP 脚本中引发错误