2011-12-01 37 views
0

我嘗試打開表格行上任意位置點擊觸發的fancybox。點擊某行後,我在Firebug控制檯實現這一消息:Click on tablerow should open a fancybox

太多的遞歸調用 http://code.jquery.com/jquery-1.7.js 線2925

這裏是我的HTML:

<table id="allItems"> 
    <tr class="itemRow"> 
     <td><a href="http://www.google.com" class="foo">foo</a></td> 
     <td>bar</td> 
    </tr> 
    <tr class="itemRow"> 
     <td><a href="http://www.bing.com" class="foo">bar</a></td> 
     <td>bar</td> 
    </tr> 
</table> 

這裏是我的javascript:

var itemRow = $('#allItems tr.itemRow'); 

itemRow.click(function(e){ 
    e.preventDefault(); 
    $(this).find('.foo').trigger('click'); 
}); 


$('.foo').fancybox({ 
    'href' : $(this).attr('href'), 
    'width'    : '100%', 
    'height'   : '100%', 
    'autoScale'   : false, 
    'transitionIn'  : 'none', 
    'transitionOut'  : 'none', 
    'type'    : 'iframe' 
}); 

,你可以在這裏找到一個活生生的例子:http://jsfiddle.net/svebal/zQ8FZ/

+0

下class是foo的元素適用於我的瀏覽器,沒有任何錯誤和可愛的fancybox。 –

+0

當你點擊「一些數據」或「另一個數據」? O.O – skamander

+0

嗯,不,看我的答案,使其工作就像那樣。 –

回答

2

.trigger()是在幾個瀏覽器/ jQuery插件相當不可靠的。你想做的事:

var itemRow = $('#allItems tr.itemRow'); 

itemRow.click(function(e) { 
    e.preventDefault(); 
    $.fancybox({ 
     'href': $(this).find('.foo').attr('href'), 
     'width': '100%', 
     'height': '100%', 
     'autoScale': false, 
     'transitionIn': 'none', 
     'transitionOut': 'none', 
     'type': 'iframe' 
    }); 
}); 
0

事實上,你唯一需要的腳本是這樣的:

$('.foo').click(function(){ 
    $.fancybox({ 
      'href' : this.href, 
      'width'    : '100%', 
      'height'   : '100%', 
      'autoScale'   : false, 
      'transitionIn'  : 'none', 
      'transitionOut'  : 'none', 
      'type'    : 'iframe' 
    }); 
    return false; 
}); 
+0

壞主意! :)它應該可以點擊整個表格中的行。 第一個單元格I(或fancybox)中的鏈接只需要href屬性。 這裏是一個更好的榜樣,我要做到:http://jsfiddle.net/svebal/DWEmU/ – skamander

+0

是的,你是對的。我忽略了一個事實,即您想單擊單元格上的任意位置,而不是單擊鏈接。我的錯。幸運的是,你對你的問題得到了很好的回答。 – JFK

0

,如果你想點擊TR任何TD後,顯示收藏夾,你必須改變這一行:

$(this).find('.foo').trigger('click'); 

這個

$(this).next().find('.foo').trigger('click'); 

這個發現在下一個元素