jQuery手风琴防止事件冒泡/允许默认链接操作。

3

我有一个手风琴设置如下:

 $('.shortheadline').accordion({ active: false, header: '.headline',  autoHeight: false, animated: 'slowslide', changestart: function(event, ui) { $('.brief').css('min-height','0') }, change: function(event, ui) {  $('.brief:visible').css('min-height','80px'); $('.headline').blur(); } });

我希望点击仅在 .headline div 上注册,而不是链接内部。链接应该带您到文章页面。
   <div class="shortheadline"> 
        <div class="entry">
            <div class="headline">
                <div class="timestamp">11:34 AM</div>
                <div class="title"><a href="http://beta.macobserver.com/tmo/article/jeff_gamet_shares_iphone_tips_and_tricks_on_macjury/">Jeff Gamet Shares iPhone Tips and Tricks on MacJury</a></div>
            </div>
            <div class="brief">
                <div class="teaser_image"> <img src="/imgs/cache/imgs/teaser_images/20090708macjury_new-0x80.png" width="80" height="80"  id="teas_48236" alt="macJuryJeff Gamet Shares iPhone Tips and Tricks on MacJury" /></div>
                <div class="teaser"><p><em>The Mac Observer's</em> Jeff Gamet joined <em>MacJury</em> host Chuck Joiner to talk about Apple's iPhone OS 3.0, the iPhone 3GS, and to share some iPhone tips and tricks, too.</p> </div>
            </div>
        </div>
    <!-- more entries -->
    </div>

有没有一种方法可以防止事件冒泡,以便手风琴仅在单击div时触发? 或者有没有一种方法允许链接的默认操作继续?
我已经尝试过:
$("a").click(function(){ window.location=this.href;  });

这样可以使链接功能正常,但不允许用户在新标签页/窗口中打开链接。

谢谢!

2个回答

12

尝试

$("a").click(function(event){ event.stopPropagation(); });

click函数会传递一个'event'对象。通过调用事件对象的'stopPropagation'方法,你可以阻止冒泡。


太棒了,谢谢!我查了“stopPropagation”,它正是我要找的东西。我甚至不需要“window.location”这一步骤。 - user139195

0

我发现了一种对我有效的替代语法;

$("a").bind("click", function(e) {e.stopPropagation();});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接