jquery悬停在一个div上工作,而不是在另一个div上工作

发布于 2024-08-17 06:27:19 字数 1403 浏览 5 评论 0原文

我有两个 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 上触发。

http://www.raceramps.com/v2

您可以通过“浏览全部”看到它,并且盘旋在上空。

感谢您的帮助!

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.

http://www.raceramps.com/v2

You can see it there by "browse all" and hovering over.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や三分注定 2024-08-24 06:27:19

Id browse-all-button 只能使用一次,因为它是一个 id。使用类来代替。例如:

<div class="car-service browse-all-button">

并且

$('.browse-all-button').hover(...

Id browse-all-button should be used only once, because it's an id. Use class instead. For example:

<div class="car-service browse-all-button">

And

$('.browse-all-button').hover(...
从﹋此江山别 2024-08-24 06:27:19

您将 browser-all-button 作为两个 div 的 ID。 ID 只能在整个页面上使用一次,类可以多次使用。将您所拥有的内容交换为 id 和类,然后使用 $(".browse-all-button")

例如

<div id="car-service" class="browse-all-button">
...
<div id="trailer-hauling" class="browse-all-button">
...
$('.browse-all-button').hover(...);

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 id="car-service" class="browse-all-button">
...
<div id="trailer-hauling" class="browse-all-button">
...
$('.browse-all-button').hover(...);
友谊不毕业 2024-08-24 06:27:19

我注意到的一件事是您在两个 div 上都使用了 browser-all-button id。这会导致 javascript 不知道你想要哪个元素。我认为您应该将 HTML 更改为如下所示。

<div id="car_service" class="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 id="trailer_hauling" class="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> 

我还将连字符更改为下划线。最好使用它们。以下是您的 javascript 在这种情况下将发生的变化。

$(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})
   });
});

希望这有帮助!

都会

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.

<div id="car_service" class="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 id="trailer_hauling" class="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> 

I also changed the hyphens to underscores. It is better use them. Here is how your javascript would change in this instance.

$(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})
   });
});

Hope this helps!

Metropolis

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文