简体   繁体   English

如何在DIV标签上使用javascript onclick来切换包含可点击链接的部分的可见性?

[英]How to use javascript onclick on a DIV tag to toggle visibility of a section that contains clickable links?

Hi I've got a DIV section that has only its title visible initially. 嗨,我有一个DIV部分,最初只能看到它的标题。 What I would like to achieve is that when the visitor clicks anywhere on the area of toggle_section the toggle_stuff div toggles between visible/hidden. 我想要实现的是当访问者点击toggle_section区域的任何地方时, toggle_stuff div在可见/隐藏之间切换。

<div id="toggle_section" 
     onclick="javascript: new Effect.toggle('toggle_stuff', 'slide');">
    <div id="toggle_title">Some title</div>
    <div id="toggle_stuff">
        some content stuff
        <a href="/foo.php">Some link</a>
    </div>
</div>

However, the way it is set-up right now, if I have any <a> link within the toggle_section , clicking that link will also execute the onclick event. 但是,它现在的设置方式,如果我在toggle_section有任何<a>链接,单击该链接也将执行onclick事件。

Then my question is what would be the best way to set this type of behavior? 那么我的问题是设置这种行为的最佳方法是什么?

The most simple solution is to add an extra onclick handler to the link within your DIV which stops event propagation: 最简单的解决方案是在DIV中为链接添加一个额外的onclick处理程序,以阻止事件传播:

<div id="toggle_section" 
     onclick="javascript: new Effect.toggle('toggle_stuff', 'slide');">
    <div id="toggle_title">Some title</div>
    <div id="toggle_stuff">
        some content stuff
        <a href="/foo.php"
            onclick="Event.stop(event);"
        >Some link</a>
    </div>
</div>

The above example uses Prototype's Event.stop() function in order to facilitate a cross browser event propagation stop. 上面的示例使用Prototype的Event.stop()函数,以便于跨浏览器事件传播停止。

As you use the inline onclick() handler, most (if not all) browser will traverse the event in the bubbling phase first (which is what you want). 当您使用内联onclick()处理程序时,大多数(如果不是全部)浏览器将首先在冒泡阶段遍历事件(这是您想要的)。

A good guide to understanding the actual reasons behind this behaviour and the differences between event capturing and event bubbling can be found at the excellent Quirksmode. 理解这种行为背后的实际原因以及事件捕获事件冒泡之间的差异的良好指南可以在优秀的Quirksmode中找到

in script : 在脚本中:

function overlayvis(blck) {
  el = document.getElementById(blck.id);
  el.style.visibility = (el.style.visibility == 'visible') ? 'hidden' : 'visible';
}

activator link, followed by content (no reason that couldn't be else on the page): 激活链接,后跟内容(没有理由不能在页面上其他):

<div onclick='overlayvis(showhideme)">show/hide stuff</div>
<div id="showhideme">
  ... content to hide / unhide ...
</div>

I got this from Modal window javascript css overlay - had to search for the source and was pleased to find it was this site. 我从Modal窗口获得了这个javascript css overlay - 不得不搜索源代码并很高兴地发现它是这个网站。 :) :)

After I posted my first question I could not wait to try to think about it once more and it seems that I have found a quite simple way to achieve this. 在我发布第一个问题之后,我迫不及待想再试一次,似乎我找到了一种非常简单的方法来实现这一点。

I have moved the onlick Effect.toggle into a separate function like: 我已将onlick Effect.toggle移动到一个单独的函数中,如:

function someClick(event) {
    if (event.target.nodeName == 'A') {
        return;
    } else {
        new Effect.toggle('toggle_stuff', 'slide');
    }
}

I suppose this would only work for A tags and not anything else that is clickable though. 我想这只适用于A标签,而不是任何可点击的标签。

I don't really know if this would work, but try giving the link element a bigger z-index value than the toggle_section div. 我真的不知道这是否可行,但尝试给link元素一个比toggle_section div更大的z-index值。

something like : 就像是 :

#toggle_section a { z-index: 10; }

Add an onclick handler to stop event propagation. 添加onclick处理程序以停止事件传播。

With JQuery use: onclick="event.stopPropagation()" 使用JQuery:onclick =“event.stopPropagation()”

With Prototype use: onclick="Event.stop(event)" 使用Prototype:onclick =“Event.stop(event)”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM