jQuery:如何在悬停时切换显示

发布于 2024-09-18 13:54:00 字数 2436 浏览 2 评论 0原文

我有 2 个跨度。

第一个: 第二个:

我的 CSS:

.desc {display: none;}

我想要做的,是在悬停第一个跨度时显示第二个跨度。没有花哨的效果或任何东西,只是显示跨度。 我知道有一个简单的 jQuery 解决方案,但我是 jQuery 新手,所以我希望得到一些帮助来找到它。 ;)

这就是我到目前为止所拥有的。语法正确吗?

<script>
$(document).ready(function() {
    $(".body").hover(function () {
        $(".desc").toggle();
    })
})
</script>

更新!

正如 @thenduks 指出的,这会导致每个 .desc 元素在 .body 悬停时显示。正如您可能想象的那样,我有几个 .body.desc,我只想在将鼠标悬停在 上时显示相应的 .desc >.body

这是我标记的一部分:(整个网站仍然只是本地的,所以我不能给你一个网址来测试 - 抱歉)

<ul class="form">
    <li>
      <label class="grid_3 alpha caption" for="from_name">Navn/Firma</label>
      <span class="grid_4 body"><input type="text" id="from_name" name="form[from_name]" size="20" value=""></span>
      <span class="grid_5 omega validation"><span class="formNoError" id="component74">Udfyld navn eller firma for afhentningsadressen.</span></span>
      <span class="grid_5 omega desc" style="display: none;"><p>Navnet på afsenderen.</p></span>
    </li>
    <li>
      <label class="grid_3 alpha caption" for="from_address1">Adresse</label>
      <span class="grid_4 body"><input type="text" id="from_address1" name="form[from_address1]" size="20" value=""></span>
      <span class="grid_5 omega validation"><span class="formNoError" id="component75">Udfyld afhentningsadressen.</span></span>
      <span class="grid_5 omega desc" style="display: none;"><p>Adressen på afsenderen.</p></span>
    </li>
    <li>
      <label class="grid_3 alpha caption" for="from_address2">Adresse 2</label>
      <span class="grid_4 body"><input type="text" placeholder="etage/lejlighedsnr./e.l." id="from_address2" name="form[from_address2]" size="20" value=""></span>
      <span class="grid_5 omega validation"><span class="formNoError" id="component76">Invalid Input</span></span>
      <span class="grid_5 omega desc" style="display: none;"><p></p></span>
    </li>
</ul>

I have 2 spans.

First one: <span class="body">
Second one: <span class="desc">

My CSS:

.desc {display: none;}

What I want to do, is show the second span, when hovering the first. No fancy effects or anything, just show the span.
I know there is a simple jQuery solution out there, but I'm new to jQuery so I'd appreciate some help finding it. ;)

This is what I have so far. Is the syntax correct?

<script>
$(document).ready(function() {
    $(".body").hover(function () {
        $(".desc").toggle();
    })
})
</script>

UPDATE!

As @thenduks pointed out, this results in every .desc element to show when a .body is hovered. As you can probably imagine, I have several .body and .desc and I only want the corresponding .desc to show on hovering the .body.

This is part of my markup: (the whole site is still just local, so I can't give you an url to test - sorry)

<ul class="form">
    <li>
      <label class="grid_3 alpha caption" for="from_name">Navn/Firma</label>
      <span class="grid_4 body"><input type="text" id="from_name" name="form[from_name]" size="20" value=""></span>
      <span class="grid_5 omega validation"><span class="formNoError" id="component74">Udfyld navn eller firma for afhentningsadressen.</span></span>
      <span class="grid_5 omega desc" style="display: none;"><p>Navnet på afsenderen.</p></span>
    </li>
    <li>
      <label class="grid_3 alpha caption" for="from_address1">Adresse</label>
      <span class="grid_4 body"><input type="text" id="from_address1" name="form[from_address1]" size="20" value=""></span>
      <span class="grid_5 omega validation"><span class="formNoError" id="component75">Udfyld afhentningsadressen.</span></span>
      <span class="grid_5 omega desc" style="display: none;"><p>Adressen på afsenderen.</p></span>
    </li>
    <li>
      <label class="grid_3 alpha caption" for="from_address2">Adresse 2</label>
      <span class="grid_4 body"><input type="text" placeholder="etage/lejlighedsnr./e.l." id="from_address2" name="form[from_address2]" size="20" value=""></span>
      <span class="grid_5 omega validation"><span class="formNoError" id="component76">Invalid Input</span></span>
      <span class="grid_5 omega desc" style="display: none;"><p></p></span>
    </li>
</ul>

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

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

发布评论

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

评论(3

豆芽 2024-09-25 13:54:00

这将导致将鼠标悬停在任何 .body 元素上,显示所有 .desc 元素。

如果您发布实际标记,我们可以找出正确的代码来为悬停的 .body 显示相应的 .desc

更新:因此,考虑到更新答案中的标记,我可能会像这样重写处理程序:

$('.body').hover( function() {
  $(this).siblings('.desc').toggle();
} );

更新2:要在单击和活动上具有相同的行为是有点棘手,因为一旦您将鼠标移出 .body,即使您单击 .desc,它也会隐藏。不过可行...我会尝试这个:

var showFunc = function() { $(this).attr('rel', 'open').siblings('.desc').show(); };
$('.body')
  .click( showFunc )
  .focus( showFunc )
  .hover(
    function() {
      // don't update 'rel' attribute
      $(this).siblings('.desc').show();
    },
    function() {
      // here's the tricky part, if the rel attr is set
      // then dont hide, otherwise, go ahead
      if( $(this).attr('rel') == 'open' ) {
        $(this).siblings('.desc').hide();
      }
    }
  );

所以这将使得只需悬停即可像以前一样隐藏/显示,但如果您单击它将“粘”打开。在这种情况下,您可能希望显示处理程序也隐藏其他打开的 .desc,这样您就不会出现大量打开的内容。因此,也许 showFunc 应该是:

var showFunc = function() {
  $("." + $(this).className).attr('rel', '');
  $(this).attr('rel', 'open').siblings('.desc').show();
};

... 这将简单地清除先前单击/聚焦元素上的所有其他 rel 属性。我确信这仍然需要调整,但希望它能为您指明正确的方向。

This will result in hovering over any .body element showing all .desc elements.

If you post your actual markup we can figure out the proper code to show a corresponding .desc for a hovered .body.

Update: So, given the markup in the updated answer I'd probably re-write the handler like so:

$('.body').hover( function() {
  $(this).siblings('.desc').toggle();
} );

Update2: To have the same behavior on click and active is a bit tricky because as soon as you mouseout of the .body the .desc will hide even if you clicked on it. Doable though... I'd try this:

var showFunc = function() { $(this).attr('rel', 'open').siblings('.desc').show(); };
$('.body')
  .click( showFunc )
  .focus( showFunc )
  .hover(
    function() {
      // don't update 'rel' attribute
      $(this).siblings('.desc').show();
    },
    function() {
      // here's the tricky part, if the rel attr is set
      // then dont hide, otherwise, go ahead
      if( $(this).attr('rel') == 'open' ) {
        $(this).siblings('.desc').hide();
      }
    }
  );

So this will make it so that just hovering hides/shows as before, but if you click it will 'stick' open. In this case you'd probably want the show handler to also hide other open .descs so that you wont end up tons of open ones all over. So maybe showFunc should be:

var showFunc = function() {
  $("." + $(this).className).attr('rel', '');
  $(this).attr('rel', 'open').siblings('.desc').show();
};

... which will simply clear all other rel attributes on previously clicked/focused elements. I'm sure this would still need tweaking, but hopefully it points you in the right direction.

指尖上得阳光 2024-09-25 13:54:00

你的脚本有效。

http://jsfiddle.net/y8wPw/

-update-
根据使用的 JQuery 版本,您的脚本以不同的方式工作。在 JQuery 1.3.2 中,当鼠标悬停在 .body 范围上时,.desc 范围将显示,但当鼠标离开它时不会消失。下次当鼠标进入.body时。该脚本反转其操作并隐藏 .desc。因此,每次您将鼠标悬停在第一个跨度上和关闭时,它都会运行脚本一次。

在 JQuery 1.4.2 中显然有一个更新,使 .desc 在鼠标悬停时显示,但在鼠标移开时立即隐藏。我想这就是你正在寻找的。

Your script works.

http://jsfiddle.net/y8wPw/

-update-
Your scripts works in different ways according to the JQuery version used. In JQuery 1.3.2 the .desc span will show when you mouseover the .body span, but will not disappear when the mouse leaves it. Next time when the mouse enters .body. The script reverses it's action and hides the .desc. So every time you hover on and off the first span, it'll run the script once.

In JQuery 1.4.2 apparently there's been an update that makes the .desc show up on mouseover, but hide immediately on mouseout. I think this is what you were looking for.

为人所爱 2024-09-25 13:54:00

如果您希望能够仅显示特定 .body 元素的 .desc 元素,您可以使用以下内容:

HTML

<div class='body'>Body content<span class='desc'>description</span></div>
<div class='body'>Body content<span class='desc'>description</span></div>
<div class='body'>Body content<span class='desc'>description</span></div>

jQuery

$(document).ready(function() {
    $(".body").hover(function () {
        $(this).find(".desc").toggle();
    })
})

CSS

.desc {
    display: none;
}

希望有帮助

If you want to be able to just show the .desc element of a specific .body element you can use the following:

HTML

<div class='body'>Body content<span class='desc'>description</span></div>
<div class='body'>Body content<span class='desc'>description</span></div>
<div class='body'>Body content<span class='desc'>description</span></div>

jQuery

$(document).ready(function() {
    $(".body").hover(function () {
        $(this).find(".desc").toggle();
    })
})

CSS

.desc {
    display: none;
}

Hope that helps

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