$(element:visible) 在 $(this) 之前或之后

发布于 2024-11-18 20:54:17 字数 810 浏览 10 评论 0原文

如何确定 HTML 中 $(element:visible) 是放置在 $(this) 之前还是之后?

我的 HTML 代码如下所示

<div class="wrapper" style="display:none;">
    <div class="title">1</div>
    <div class="text">Lorem Ipsum</div>
</div>

<div class="wrapper">
    <div class="title">2</div>
    <div class="text">Lorem Ipsum</div>
</div>

<div class="wrapper" style="display:none;">
    <div class="title">3</div>
    <div class="text">Lorem Ipsum</div>
</div>

<div class="wrapper" style="display:none;">
    <div class="title">4</div>
    <div class="text">Lorem Ipsum</div>
</div>

当我单击标题 2 时,我想知道该标题之前是否有可见的 $('.wrapper')

How do I determine if $(element:visible) is placed before or after $(this) in HTML?

My HTML code looks like this

<div class="wrapper" style="display:none;">
    <div class="title">1</div>
    <div class="text">Lorem Ipsum</div>
</div>

<div class="wrapper">
    <div class="title">2</div>
    <div class="text">Lorem Ipsum</div>
</div>

<div class="wrapper" style="display:none;">
    <div class="title">3</div>
    <div class="text">Lorem Ipsum</div>
</div>

<div class="wrapper" style="display:none;">
    <div class="title">4</div>
    <div class="text">Lorem Ipsum</div>
</div>

When I click on title 2 I want to know if there is visible $('.wrapper') before that title.

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

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

发布评论

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

评论(6

仅此而已 2024-11-25 20:54:17

我希望这就是您所需要的。

$(".title").click(function(){
  if($(this).parent().prev().is(":visible")) alert("Previous is visible!")
});

这会告诉您之前的 .wrapper (单击的标题之前)是否可见。

编辑:这解决了您更新的问题

$(".title").click(function(){
  if($(this).parent().prevAll(".wrapper").is(":visible")) alert("Previous is visible!")
});

希望这有帮助。干杯

I hope this is what you need

$(".title").click(function(){
  if($(this).parent().prev().is(":visible")) alert("Previous is visible!")
});

This tells you if the previous .wrapper (previous to clicked title) is visible.

EDIT: This solves your updated question

$(".title").click(function(){
  if($(this).parent().prevAll(".wrapper").is(":visible")) alert("Previous is visible!")
});

Hope this helps. Cheers

梦里人 2024-11-25 20:54:17

总结一下。您需要检查在单击的元素之前是否存在任何具有“wrapper”类的可见元素。

如果是这样的话,应该可以做到这一点。

$(".wrapper").click(function() {
  var el;
  el = $(this);
  $(".wrapper:visible").each(function(index) {
    if ( el.get(0) === $(this).get(0) ) {
      alert("There are no visible wrappers prior to this one.");
      return false;
    } else {
      alert("There are visible wrappers prior to this one.");
      return false;
    }
  });
});

或者:

$(".wrapper").click(function() {
  if ( $(this).prevAll(".wrapper:visible").length ) {
    return alert("There is " + $(this).prevAll(".wrapper:visible").length  + " wrappers prior to this one.");
  } else {
    return alert("There are no visible wrappers prior to this one.");
  }
});

To sum it up. You need to check if there are any visible elements with class "wrapper" prior to clicked element.

If that is it, this should do it.

$(".wrapper").click(function() {
  var el;
  el = $(this);
  $(".wrapper:visible").each(function(index) {
    if ( el.get(0) === $(this).get(0) ) {
      alert("There are no visible wrappers prior to this one.");
      return false;
    } else {
      alert("There are visible wrappers prior to this one.");
      return false;
    }
  });
});

Or:

$(".wrapper").click(function() {
  if ( $(this).prevAll(".wrapper:visible").length ) {
    return alert("There is " + $(this).prevAll(".wrapper:visible").length  + " wrappers prior to this one.");
  } else {
    return alert("There are no visible wrappers prior to this one.");
  }
});
朮生 2024-11-25 20:54:17
$(this).parent("div:visible")

仅当父级可见时,它才会为您提供父级。

$(this).parent("div:visible")

This will give you a parent only if it is visible.

森林迷了鹿 2024-11-25 20:54:17
$(this).closest('.wrapper').prev().is(':visible');

将获得带有类 wrapper 的最接近的父级,这意味着即使 $(this) 位于包装器内部较深处,它仍然可以正常运行。

$(this).closest('.wrapper').prev().is(':visible');

Will get the closest parent with a class wrapper meaning even if $(this) is deeper inside the wrapper it will still function properly.

妄断弥空 2024-11-25 20:54:17

如果您想查看是否有任何以前可见的包装纸......
$(this).parent().prev(".wrapper:visible").length()

如果您想查看是否只有前一个可见...
$(this).parent().prev().is(":visible")

If you are looking to see if there is ANY previous visible wrappers...
$(this).parent().prev(".wrapper:visible").length()

If you are looking to see if only the previous one before it is visible...
$(this).parent().prev().is(":visible")

初见终念 2024-11-25 20:54:17
$('.title').click(function() {
    if ($(this).parent().prev('.wrapper:visible').length) {
        console.log('visible');
    } else {
        console.log('not visible');
    }
});
$('.title').click(function() {
    if ($(this).parent().prev('.wrapper:visible').length) {
        console.log('visible');
    } else {
        console.log('not visible');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文