如何排除不受欢迎的后代?

发布于 2024-10-11 05:36:15 字数 3292 浏览 2 评论 0原文

我遇到的情况是,一个元素包含 n 个可点击句柄和 n 个可显示元素:

<div class="revealer">
  <div class="hotspot">
    <a class="handle" href="javascript:;">A</a>
    <div class="reveal">
      <p>Content A.</p>
    </div>
    <div class="reveal">
      <p>Content B.</p>
    </div>
  </div>
</div>

当我单击“句柄”时,它会显示两个“显示”元素。这很好用。这段代码会被放入给定文档中创建者想要的任何位置。包括...在另一个

元素内。嵌套这些揭示者对象的能力是合理的,并且对我来说很有用。

我所坚持的是一种只处理直接

元素的好方法,而不是嵌套的元素(否则单击外部句柄将显示所有嵌套的显示)。

所以这里是示例结构:


<div class="revealer">
  <div class="hotspot">
    <a class="handle" href="javascript:;">A</a>
    <div class="reveal">
      <p>Content A.</p>
    </div>
    <div class="reveal">

        <p>Content B.</p>

        <!-- nested revealer -->
        <div class="revealer">
          <div class="hotspot">
            <a class="handle" href="javascript:;">A</a>
            <div class="reveal">
              <p>Sub-content A.</p>
            </div>
            <div class="reveal">
              <p>Sub-content B.</p>
            </div>
          </div>
        </div>
        <script type="text/javascript"> // a setup script which instantiates a Module object for this revealer, which controls all revealing </script>

    </div>
  </div>
</div>
<script type="text/javascript"> // a setup script, which instantiates a Module object for this revealer, which controls all revealing </script>

我大量使用 YUI2.8.2 框架,所以现在我有一个测试,当你单击一个句柄来收集一组它自己的揭示者并显示它们时,但不包括嵌套的揭示,这应该被执行由他们自己的实例化模块,而不是父母的实例化模块。

Javascript 测试如下:

    // grab all 'reveal' elements to show
    var reveals = yd.getElementsBy(function(el){
            if( yd.hasClass(el, REVEAL_CLASS) && pObj.isChild( el ) ){
                return true ;
            }
            return false;
    }, null, this.root() );


    // the test method above is "isChild( el )", the 'el' arg is a 'handle' inside a 'hotspot', so I have...

isChild: function( el )
{
    var ancestor = yd.getAncestorBy( el, function(nestedEl){
        return yd.hasClass(nestedEl, REVEALER_CLASS);
    });

    // ancestor is the immediate parent 'reveal' element
    var forefather = yd.getAncestorBy( ancestor, function(nestedEl){
        return yd.hasClass(nestedEl, REVEALER_CLASS);
    });

    // forefather is 
    if(forefather){
        // Yuck yuck yuck, get rid of this dependency on a custom className :(
        if(!yd.hasClass(this.getRoot(), 'revealer-nested') ){
            return false ;
        }
    }
    return true ;
},

但我所能做的就是向嵌套的 revealer 元素添加一个新的类名 revealer-nested...但是我 真的不想这样做,因为这些对象应该存在于它们自己的上下文中,而不关心或受到父揭示者的影响。

...我希望这不是 tmi,请询问任何必需的问题等以获取更多信息 - 我可能错过了一些上下文信息,因为我正在重构此模块。

非常非常感谢。

编辑: 同样重要的是,我不要开始依赖诸如parentNode、childNode[x]、nextSibling 等后代属性...因为目前这个模块非常灵活,因为它的“reveal”和“handle”元素可以驻留在其他元素中标记并仍然成为目标 - 只要它们被发现在“热点”内。

I have a situation where an element contains n clickable handles and n revealable elements:

<div class="revealer">
  <div class="hotspot">
    <a class="handle" href="javascript:;">A</a>
    <div class="reveal">
      <p>Content A.</p>
    </div>
    <div class="reveal">
      <p>Content B.</p>
    </div>
  </div>
</div>

When I click 'handle', it shows both 'reveal' elements. This works fine. This chunk of code is dropped into a given document wherever the creator wants. Including... inside another <div class="reveal"></div> element. The ability to nest these revealer objects is reasonable, and useful in my case.

What I'm stuck on, is a decent way to only handle only immediate <div class="reveal"></div> elements, and not nested ones (otherwise clicking on the outer handle would reveal alll nested reveals).

So here's the example structure:


<div class="revealer">
  <div class="hotspot">
    <a class="handle" href="javascript:;">A</a>
    <div class="reveal">
      <p>Content A.</p>
    </div>
    <div class="reveal">

        <p>Content B.</p>

        <!-- nested revealer -->
        <div class="revealer">
          <div class="hotspot">
            <a class="handle" href="javascript:;">A</a>
            <div class="reveal">
              <p>Sub-content A.</p>
            </div>
            <div class="reveal">
              <p>Sub-content B.</p>
            </div>
          </div>
        </div>
        <script type="text/javascript"> // a setup script which instantiates a Module object for this revealer, which controls all revealing </script>

    </div>
  </div>
</div>
<script type="text/javascript"> // a setup script, which instantiates a Module object for this revealer, which controls all revealing </script>

I heavily use the YUI2.8.2 framework, so right now I have a test in place when you click a handle to collect a set of its own revealers and show them, but excluding nested reveals, which should be actioned by their own instantiated module, not the parents'.

The Javascript test is as follows:

    // grab all 'reveal' elements to show
    var reveals = yd.getElementsBy(function(el){
            if( yd.hasClass(el, REVEAL_CLASS) && pObj.isChild( el ) ){
                return true ;
            }
            return false;
    }, null, this.root() );


    // the test method above is "isChild( el )", the 'el' arg is a 'handle' inside a 'hotspot', so I have...

isChild: function( el )
{
    var ancestor = yd.getAncestorBy( el, function(nestedEl){
        return yd.hasClass(nestedEl, REVEALER_CLASS);
    });

    // ancestor is the immediate parent 'reveal' element
    var forefather = yd.getAncestorBy( ancestor, function(nestedEl){
        return yd.hasClass(nestedEl, REVEALER_CLASS);
    });

    // forefather is 
    if(forefather){
        // Yuck yuck yuck, get rid of this dependency on a custom className :(
        if(!yd.hasClass(this.getRoot(), 'revealer-nested') ){
            return false ;
        }
    }
    return true ;
},

But all I can muster is to add a new class name, revealer-nested, to a nested revealer element... but I really don't want to have to do that, because these objects are supposed to exist in their own context and not care or be affected by parent revealers.

... I hope that isn't tmi, please ask any required questions etc for more info - I may have missed out some contextual info as I'm right in the middle of refactoring this module.

Many, many thanks in advance.

EDIT:
It's also quite important that I don't start relying on descendant properties like parentNode, childNode[x], nextSibling, etc ... because currently this module is quite flexible in that its 'reveal' and 'handle' elements can reside within other markup and still be targeted - so long as they're found inside a 'hotspot'.

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

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

发布评论

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

评论(1

陌路终见情 2024-10-18 05:36:15

CSS3 选择器 :not() 允许您从选择中过滤掉元素。我知道 jQuery 可以通过 CSS3 选择器选择一组元素,例如:

$('.revealer:not(.revealer > .revealer)')

我认为它可以作为选择器,仅选择“revealer”类的元素,而不是“revealer”类的子元素。

这一切都取决于 YUI 能够支持 CSS3 选择器来选择元素集(如果您肯定只使用 YUI)。

这有帮助吗?我想这就是你问的...

There is a CSS3 selector :not() which allows you to filter out elements from a selection. I know jQuery can select a set of elements by CSS3 selector, for example:

$('.revealer:not(.revealer > .revealer)')

I think that would work as a selector to only select elements of class "revealer" which aren't children of class "revealer".

This all depends on YUI being able to support CSS3 selectors to select sets of elements (if you're definitely only using YUI).

Does that help? I think that's what you were asking...

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