jquery accordion阻止冒泡/允许默认链接操作

时间:2009-07-16 05:03:37

标签: jquery jquery-ui accordion event-bubbling

我有手风琴设置如下:

 $('.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 个答案:

答案 0 :(得分:12)

尝试

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

点击功能传递'事件'对象。通过调用事件对象的“stopPropagation”方法,可以防止冒泡。

答案 1 :(得分:0)

我遇到过这种替代语法,对我有用;

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