jquery - 如何设置父属性?

发布于 2024-08-14 17:08:03 字数 714 浏览 3 评论 0原文

我正在尝试编写一个 if 语句,其中如果其中一个元素的显示设置为“无”,我希望父元素也显示“无”...

这是我正在尝试的代码,它不不起作用...

/* tried this first */
if($('#prevx a').attr('display') == 'none') {
    $(this).parent().attr('display','none');
}

/* and then this */
if($('#prevxa > a').attr('display') == 'none') {
    $('#prevxa').attr('display','none');
}

标记如下所示:

<ul>
   <li class="navnext" id="nextxa">
      <a id="nextx" href="#"><img src="/images/next.png"/></a>
   </li>

   <li class="navprev" id="prevxa">
      <a id="prevx" href="#" style="display: none;"><img src="/images/previous.png"/></a>
   </li>
</ul>

I'm trying to write an if statement where if one of the elements has it's display set to "none", I want the parent element to also display "none"...

This is the code I'm trying, which doesn't work...

/* tried this first */
if($('#prevx a').attr('display') == 'none') {
    $(this).parent().attr('display','none');
}

/* and then this */
if($('#prevxa > a').attr('display') == 'none') {
    $('#prevxa').attr('display','none');
}

The markup looks like this:

<ul>
   <li class="navnext" id="nextxa">
      <a id="nextx" href="#"><img src="/images/next.png"/></a>
   </li>

   <li class="navprev" id="prevxa">
      <a id="prevx" href="#" style="display: none;"><img src="/images/previous.png"/></a>
   </li>
</ul>

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

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

发布评论

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

评论(5

蹲在坟头点根烟 2024-08-21 17:08:03

试试这个:

if($('#prevx').css('display') == 'none') {
    $('#prevx').parent().css('display','none');
}

更好的是:

$('#prevx').parent().css('display',$('#prevx').css('display'));

这个例子对我有用。要隐藏/显示父级,请在无和内联之间切换子级的显示:

<ul>
   <li class="navnext" id="nextxa">
      <a id="nextx" href="#"><img src="/images/next.png"/></a>
   </li>

   <li class="navprev" id="prevxa">
      <a id="prevx" href="#" style="display: inline;"><img src="/images/previous.png"/></a>
   </li>
</ul>

<script>
if ($('#prevx').css('display') == 'none') 
    $('#prevx').parent().css('display', 'none');
else
    $('#prevx').parent().css('display', 'list-item');
</script>

Try this:

if($('#prevx').css('display') == 'none') {
    $('#prevx').parent().css('display','none');
}

Better yet:

$('#prevx').parent().css('display',$('#prevx').css('display'));

This example works for me. To hide/display the parent, toggle the child's display between none and inline:

<ul>
   <li class="navnext" id="nextxa">
      <a id="nextx" href="#"><img src="/images/next.png"/></a>
   </li>

   <li class="navprev" id="prevxa">
      <a id="prevx" href="#" style="display: inline;"><img src="/images/previous.png"/></a>
   </li>
</ul>

<script>
if ($('#prevx').css('display') == 'none') 
    $('#prevx').parent().css('display', 'none');
else
    $('#prevx').parent().css('display', 'list-item');
</script>
放肆 2024-08-21 17:08:03

.attr 用于标签,如 id、title、alt、src 等。.css

用于 CSS 样式,如显示、字体、文本装饰:闪烁等。Pygorex1

的解决方案应该可以解决问题。

.attr is used for tags such as id, title, alt, src, etc.

.css is used for CSS styles such as display, font, text-decoration: blink, etc.

Pygorex1's solution ought to do the trick.

得不到的就毁灭 2024-08-21 17:08:03
if($('#prevx a').css('display') === 'none') {
    $(this).parent().css('display','none');
}
if($('#prevx a').css('display') === 'none') {
    $(this).parent().css('display','none');
}
紅太極 2024-08-21 17:08:03

从您的代码示例中,您已经有了父项的 ID,那么为什么不使用它呢?

此外,还有许多其他方法可以做到这一点(将下面的行包装在 $(document).ready(function(){...}) 中)以使它们起作用

: href="http://docs.jquery.com/Selectors/hidden" rel="nofollow noreferrer">:hidden 选择器(这只会隐藏):

if ($('#prevx').is(':hidden')) $('#prevxa').hide();

使用 三元运算符(这将隐藏或显示父级)

var showOrHide = ($('#prevx').is(':hidden')) ? 'none' : '';
$('#prevxa').css('display', showOrHide);

JQuery 使用一些快捷方式:

  • :hidden< /code> 将查找隐藏的对象(或设置为显示:无)。它也不必位于 .is() 内。您可以使用:$('div:hidden').show()来显示所有隐藏的div。
  • :visible 将查找未隐藏的对象(display = ''、'inline'、'block' 等)
  • $(element).hide() 将隐藏element (将显示设置为无)
  • $(element).show() 将显示一个元素(清除显示值)

From your code examples, you already have an ID for the parent, so why not use it?

Also, there are many other ways to do this (wrap the lines below inside a $(document).ready(function(){...})) to make them work:

Using a :hidden selector (this will only hide):

if ($('#prevx').is(':hidden')) $('#prevxa').hide();

Use a ternary operator (this will hide or show the parent)

var showOrHide = ($('#prevx').is(':hidden')) ? 'none' : '';
$('#prevxa').css('display', showOrHide);

JQuery uses a few shortcuts:

  • :hidden will find objects that are hidden (or are set to display: none). It doesn't have to be inside an .is() either. You can use this: $('div:hidden').show() to reveal all hidden divs.
  • :visible will find objects that are not hidden (display = '', 'inline', 'block', etc)
  • $(element).hide() will hide an element (sets display to none)
  • $(element).show() will show an element (clears display value)
如果没有 2024-08-21 17:08:03

试试这个

$('#child_id').parent().css('display','none');

Try This

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