jquery悬停在一个div上工作,而不是在另一个div上工作
我有两个 div,如下所示:
<div class="car-service" id="browse-all-button">
<p>CAR SERVICE</p>
<span>56" Race Ramps</span>
<span>67" Race Ramps XTs</span>
<span>XTenders</span>
<span>40" Sport Ramps</span>
<span>Roll-Ups</span>
<span>Portable Pit Stop</span>
</div>
<div class="trailer-hauling" id="browse-all-button">
<p>TRAILER HAULING</p>
<span>56" Race Ramps</span>
<span>67" Race Ramps XTs</span>
<span>XTenders</span>
<span>40" Sport Ramps</span>
<span>Roll-Ups</span>
<span>Portable Pit Stop</span>
</div>
Jquery 如下:
$(function(){
$('#browse-all-button').hover(function(){
$("p", this).stop().animate({textIndent:"25px", color:"#a12324"}, {duration:200});
alert($(this).attr("class"));
},
function(){
$("p", this).stop().animate({textIndent:"10px", color:"#424242"}, {duration:150})
})
} );
效果适用于第一个 #browse-all-button,但不适用于第二个。我在那里有一个警报告诉我当前的 div 类是什么,它甚至没有在第二个 div 上触发。
您可以通过“浏览全部”看到它,并且盘旋在上空。
感谢您的帮助!
I have two divs as follows:
<div class="car-service" id="browse-all-button">
<p>CAR SERVICE</p>
<span>56" Race Ramps</span>
<span>67" Race Ramps XTs</span>
<span>XTenders</span>
<span>40" Sport Ramps</span>
<span>Roll-Ups</span>
<span>Portable Pit Stop</span>
</div>
<div class="trailer-hauling" id="browse-all-button">
<p>TRAILER HAULING</p>
<span>56" Race Ramps</span>
<span>67" Race Ramps XTs</span>
<span>XTenders</span>
<span>40" Sport Ramps</span>
<span>Roll-Ups</span>
<span>Portable Pit Stop</span>
</div>
Jquery is a follows:
$(function(){
$('#browse-all-button').hover(function(){
$("p", this).stop().animate({textIndent:"25px", color:"#a12324"}, {duration:200});
alert($(this).attr("class"));
},
function(){
$("p", this).stop().animate({textIndent:"10px", color:"#424242"}, {duration:150})
})
} );
The Effect works on the first #browse-all-button, but not on the second. I have an alert in there to tell me what the current div class is, and it's not even triggered on the second div.
You can see it there by "browse all" and hovering over.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Id
browse-all-button
只能使用一次,因为它是一个 id。使用类来代替。例如:并且
Id
browse-all-button
should be used only once, because it's an id. Use class instead. For example:And
您将
browser-all-button
作为两个 div 的 ID。 ID 只能在整个页面上使用一次,类可以多次使用。将您所拥有的内容交换为 id 和类,然后使用$(".browse-all-button")
例如
You have
browser-all-button
as the ID for both divs. ID's should only be used once on the entire page, classes can be used more than once. Swap what you have for id's and classes then then use$(".browse-all-button")
e.g.
我注意到的一件事是您在两个 div 上都使用了 browser-all-button id。这会导致 javascript 不知道你想要哪个元素。我认为您应该将 HTML 更改为如下所示。
我还将连字符更改为下划线。最好使用它们。以下是您的 javascript 在这种情况下将发生的变化。
希望这有帮助!
都会
One thing I notice is you are using the browse-all-button id on both divs. This would cause the javascript to not know which element you want. You should change your HTML to look like the following I believe.
I also changed the hyphens to underscores. It is better use them. Here is how your javascript would change in this instance.
Hope this helps!
Metropolis