当你悬停.container时,它会改变两者的颜色.但我只想改变鼠标所在的容器
When you hover the .container it changes the color of both. But I just want to change it of the container where the mouse is on
我准备了这个:
http://jsfiddle.net/hXpWh/2/当你悬停。container时,它会改变两者的颜色。但是我只想改变鼠标所在的容器
下面是js代码:moped = "";
$(".container").mouseenter(function () {
$(".content").css('background', function () {
moped = $(this).css('background');
return "green";
});}).mouseleave(function () {
$(".content").css('background', function () {
return moped;
});
});
html: <div class="container">
<div class="content"></div>
<div class="caption">
<p>This is the caption of .container</p>
</div>
</div>
<div class="container2">
<div class="content"></div>
<div class="caption">
<p>This is the caption of .container2</p>
</div>
</div>
css: .container {
position: absolute;
top: 0;
left: 0;
display: block;
z-index: 800;
width: 250px;
height: 250px;
padding: 0;
margin: 0;
}
.container2 {
position: absolute;
top: 0;
left: 255px;
display: block;
z-index: 800;
width: 250px;
height: 250px;
padding: 0;
margin: 0;
}
.content {
display: block;
background: red;
position: absolute;
z-index: 900;
top: 0;
left: 0;
width: 250px;
height: 250px;
}
.caption {
display: block;
background: none;
position: absolute;
z-index: 1000;
top: 0;
left: 0;
width: 250px;
height: 250px;
}
.caption p {
position: relative;
bottom: 10px;
left: 10px;
}
其他答案显示了jQuery代码中的错误,但另一个修复方法是使用CSS来解决这个问题。
给外部元素一个公共类,然后:
.cont {
background:red;
}
.cont:hover .content {
background: green;
}
演示: http://jsfiddle.net/hXpWh/4/
但是对于jQuery代码,您不仅需要找到嵌套的.content
,而且也不需要变量。只需在mouseleave
中设置背景为""
。
$(".container").mouseenter(function () {
$(this).find(".content").css('background', "green");
}).mouseleave(function () {
$(this).find(".content").css('background', "");
});
在.mouseenter
函数中将$(".content")
更改为$(this).find(".content")
,它只会更改您悬停在上面的那个。您可以将其更改为$(".content", this)
,但根据评论中的epascarello,它不是那么有效。
你可以移动css background属性或者这样做:
moped = "";
$(".container").mouseenter(function () {
$(this).children(".content").css('background', function () {
moped = $(this).css('background-color');
return "green";
});
}).mouseleave(function () {
$(this).children(".content").css('background', function () {
return moped;
});
});
我的建议是用脚本做并重构它,使用.hover()
并分别命名mouseenter
和mouseout
函数。祝你好运,伙计。
相关文章:
- 我只想在给出条件时调用一些循环.但这是错误
- 我只想在卡片悬停上悬停动画,而不是在身体上
- 如果我只想从数组中打印任何一个元素.任何数组.那么代码会是什么
- 当我只想要1时,获取返回的所有错误消息
- 只想在页面上未加载初始数据时显示加载gif
- 如何使用javascript从数据库中获取值,我使用的是jquery自动完成,我只想从数据库中搜索值
- 只想在用户单击 html 表单上的“提交”时显示绿色复选标记
- 检查时间冲突,只想显示 1 个警告
- 只想在WordPress中使用jQuery在主页上加载弹出窗口
- 如果我只想在我的应用程序中使用表单生成器,则 Alloy UI 的 zip 文件中要包含哪些文件夹
- 我只想在警报中打印数据,但每当它提醒我 [对象对象] 但我希望它会显示我的表数据行
- 只想在一定时间后页面重定向到所需的链接
- 我只想在输入某些文本时更改其颜色
- 我只想在 Iframe 中加载(实时)源 url 的正文
- 回调被多次调用,但我只想要最后一个回调
- 我只想让我的页面刷新一次
- Div 显示:无 - 只想在内容可见时加载内容
- 我只想要在 php 文件中通过 AngularJS 调用 Ajax 后的成功/失败消息.但是我也得到了带有标签的完整HT
- 离子事件侦听器在每个屏幕上触发,只想应用于一个控制器
- 当你悬停.container时,它会改变两者的颜色.但我只想改变鼠标所在的容器