jQuery - 更改单个 DIV 的 CSS
jQuery - Change CSS of a single DIV
我有这个HTML代码:
<div id="cont">
<div class="us"></div>
<div class="us"></div>
<div class="us"></div>
</div>
和这个JS代码:
$(".us").mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
问题是这三个要素发生了变化。我在没有 cont DIV 的情况下尝试了它并且工作得很好,只更改了一个元素,但我需要它。另外,我尝试向DIV提供单个ID,但一直失败。
您可以通过
多种方式选择一个项目。
//the first one
$(".us").first().mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
//the second one
$(".us").eq(1).mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
//the third one
$(".us:nth-child(3)").mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
这里的问题在于您的 CSS 代码,因为div
元素就像一个块一样,您可以通过添加margin
来增加所有容器,而其他元素也会占用空间。您可以添加以下内容以使每个div
添加边距:
.us {
float:right;
clear:right;
}
您的演示 http://jsfiddle.net/6H8f4/14/
试试这个:
$(".us:first-child").mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
或
$(".us:last-child").mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
或
$(".us:nth-child(index)").mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
做了一个小提琴
$(".us").mouseenter(function() {
$(this).animate({'margin-right': 100}, "fast");
});
$(".us").mouseout(function() {
$(this).animate({'margin-right': 5}, "fast");
});
http://jsfiddle.net/QjL9J/1/
使用动画时,默认情况下将整数设置为值将使它们成为PX
此外,CSS 属性应该在 Animate 对象的 '' 中,并写入实际属性(margin-right 而不是 marginRight)
这应该有效,您不需要手动为元素创建每个元素,它是在引擎盖下完成的。
这是
你想要的吗?
<div id="cont">
<div class="us">asdfafsdf</div>
<div class="us">343464</div>
<div class="us">asdfdsfds</div>
</div>
jquery
$('#cont').children(".us").each(function() {
$(this).mouseenter(function() {
$(this).animate({marginRight: "10px"}, "fast");
});
});
相关文章:
- 通过DRAGGABLE将一个DIV移动到另一个DIF下?(CSS,JQuery)
- 更改CSS以允许使用Javascript控制DIV位置
- 使用CSS打印DIV内容,不起作用
- 需要帮助调整 Fiddle 代码以使用 Javascript 和 CSS 折叠/展开 Div
- jQuery - 更改单个 DIV 的 CSS
- 在悬停时显示一个DIV,在悬停的另一个DIV.使用CSS
- 无法在DIV上使用JavaScript执行CSS代码
- 如何设置DIV背景图像css jquery
- CSS Div 类之间的转换仅以一种方式发生
- 如何CSS沙盒/重置当前页面中的整个DIV区域
- JS切换DIV's第一次点击时CSS未启动
- 用CSS居中内联块DIV“:在“;以垂直和水平为中心的废墟文本
- JavaScript,CSS:剪辑到父DIV内部
- 仅当 DIV 1 低于特定分辨率时,才更改 DIV 2 和 3 的 CSS
- CSS 和 HTML:如何创建单击时展开的 DIV
- 无法将页面 CSS 应用于 DIV 对于 stackoverflow.com 中的每个主题
- 如何使用 JavaScript 动态设置 DIV 元素的左侧和顶部 CSS 位置
- 添加/删除 CSS 类属性以尊重不基于 Div id 的类
- Div CSS在悬停时不改变
- 更改文档滚动上的Div Css