如何减少 jQuery 函数中使用的子级数量?

发布于 2024-09-25 14:57:04 字数 653 浏览 0 评论 0原文

我觉得我必须在一些 jQuery 函数中使用太多的 .children()

这是我的 HTML:

<div class="goal-small-container">
  <div class="goal-content">
    <div class="goal-row">
      <span class="goal-actions">

这是我的 jQuery:

$('.goal-small-container').hover(function() {
  $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});
}, function () {
  $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "hidden"});
});

有更好的方法吗? 如何减少 jQuery 函数中使用的子级数量?

I feel like I have to use way too many .children() in some of my jQuery functions.

Here's my HTML:

<div class="goal-small-container">
  <div class="goal-content">
    <div class="goal-row">
      <span class="goal-actions">

And here's my jQuery:

$('.goal-small-container').hover(function() {
  $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});
}, function () {
  $(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "hidden"});
});

Is there a better way? How can I reduce the amount of children I use in my jQuery functions?

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

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

发布评论

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

评论(5

与君绝 2024-10-02 14:57:04
.find('.goal-content .goal-row .goal-action').whatever()

或者更简单地说:

.find('.goal-action').whatever()
.find('.goal-content .goal-row .goal-action').whatever()

or more simply:

.find('.goal-action').whatever()
北方的巷 2024-10-02 14:57:04

您听说过 .find() 吗?

$('.goal-small-container').hover(function() {
  $(this).find('.goal-actions').css({visibility: "visible"});
}, function () {
  $(this).find('.goal-actions').css({visibility: "hidden"});
});

have you heard about .find() ?

$('.goal-small-container').hover(function() {
  $(this).find('.goal-actions').css({visibility: "visible"});
}, function () {
  $(this).find('.goal-actions').css({visibility: "hidden"});
});
心作怪 2024-10-02 14:57:04

代替

$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});

您可以使用:

$(this).find('> .goal-content > .goal-row > .goal-actions').css({visibility: "visible"});

: 具有完全相同的含义。但是,如果不可能出现歧义,(.goal-actions 只会出现在该标记结构中),您只需使用 find('.goal-actions')< /代码>。

Instead of

$(this).children('.goal-content').children('.goal-row').children('.goal-actions').css({visibility: "visible"});

You can use:

$(this).find('> .goal-content > .goal-row > .goal-actions').css({visibility: "visible"});

For exactly the same meaning. If there's no chance of that being ambiguous, however, (.goal-actions will only appear in that structure of the markup) you can just use find('.goal-actions').

朱染 2024-10-02 14:57:04

你可以只使用:

$('.goal-small-container').hover(function() {
   $(this).find('goal-actions').show();
}, function() {
   $(this).find('goal-actions').hide();
});

You can just use:

$('.goal-small-container').hover(function() {
   $(this).find('goal-actions').show();
}, function() {
   $(this).find('goal-actions').hide();
});
美人骨 2024-10-02 14:57:04

为什么不在第二个

上使用 .show() 和 .hide() ?并且,最初让它们显示隐藏或其他东西。

Why don't you just use .show() and .hide() on the second <div>? And, initially have them display hidden or something.

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