Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-更改单个DIV的CSS_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript jQuery-更改单个DIV的CSS

Javascript jQuery-更改单个DIV的CSS,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有以下HTML代码: <div id="cont"> <div class="us"></div> <div class="us"></div> <div class="us"></div> </div> 问题是这三个要素发生了变化。我在没有contDIV的情况下尝试了它,效果非常好,只更改了一个元素,但我需要它。此外,我尝试为div提供单个id,但一直失败。有很多方法可以

我有以下HTML代码:

<div id="cont">
    <div class="us"></div>
    <div class="us"></div>
    <div class="us"></div>
</div>

问题是这三个要素发生了变化。我在没有contDIV的情况下尝试了它,效果非常好,只更改了一个元素,但我需要它。此外,我尝试为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");
});

有许多方法可以选择一个项目

//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");
});
试试这个:

$(".us:first-child").mouseenter(function() {
    $(this).animate({marginRight: "10px"}, "fast");
});

试试这个:

$(".us:first-child").mouseenter(function() {
    $(this).animate({marginRight: "10px"}, "fast");
});

这是你想要的吗

<div id="cont">
    <div class="us">asdfafsdf</div>
    <div class="us">343464</div>
    <div class="us">asdfdsfds</div>
</div>
这是你想要的吗

<div id="cont">
    <div class="us">asdfafsdf</div>
    <div class="us">343464</div>
    <div class="us">asdfdsfds</div>
</div>
拉小提琴

$(".us").mouseenter(function() {
    $(this).animate({'margin-right': 100}, "fast");
});

$(".us").mouseout(function() {
    $(this).animate({'margin-right': 5}, "fast");
});

当使用动画设置ints作为值时,默认情况下将使其为px

css属性也应位于动画对象的“”中,并写入实际属性(边距右侧而非边距右侧)

这应该是可行的,你不需要手动为每个元素创建一个,它是在引擎盖下用这个来完成的。

做了一把小提琴

$(".us").mouseenter(function() {
    $(this).animate({'margin-right': 100}, "fast");
});

$(".us").mouseout(function() {
    $(this).animate({'margin-right': 5}, "fast");
});

当使用动画设置ints作为值时,默认情况下将使其为px

css属性也应位于动画对象的“”中,并写入实际属性(边距右侧而非边距右侧)


这应该是可行的,你不需要手动为每个元素创建一个空格,这是在引擎盖下完成的。

这里的问题是你的CSS代码,因为
div
元素就像一个块,你通过添加
边距来增加所有的容器,其他的元素也占据了空间。您可以添加此选项,使每个
div
添加边距:

.us {
  float:right;
  clear:right;
}

您的演示

这里的问题在于CSS代码,因为
div
元素在一起就像一个块,您通过添加
边距
增加了所有容器,其他元素也占用了空间。您可以添加此选项,使每个
div
添加边距:

.us {
  float:right;
  clear:right;
}

您的演示

“持续失败”如何,当您在每个
div
?上都有ID或使用.eq(indexNumberofElement)时,问题不在于Jquery的代码,大多数情况下都有一些结构或CSS,因为我认为您是对的。是
浮点:右导致其失败的原因。那么,有什么替代浮动的方法吗?让我们看看你的代码,试着像我提供的链接一样复制它“不断失败”,当你在每个
div
上都有ID时,或者使用.eq(indexNumberofElement)问题不在于Jquery的代码,大多数代码都有一些结构或CSS,因为对我来说你是对的。是
浮点:右导致其失败的原因。那么,除了float还有什么替代方法吗?让我们看看你的代码,试着像我提供的链接一样复制它idn't work:(这很奇怪,如果我给三个div中的一个ID,然后我尝试使用它($(“#us1”)而不是$(“.us”))也失败了。你能在其中修改一下你的代码吗?这可能是css问题。我想知道jquery是否正常工作,但css让它看起来不正常?@user2223034问题不是jquery函数或选择器……问题是你的结构和css显示。不正常:(奇怪的是,如果我给三个div中的一个ID,然后我尝试使用它($(“#us1”)而不是$(“.us”))也失败了。你能修改一下里面的代码吗?这可能是css问题。我想知道jquery是否正常工作,但css让它看起来不正常?@user2223034问题不是jquery函数或选择器……问题是你的结构和css显示了这一点。