jQueryUI选项卡 - 深入链接到选项卡内容

时间:2011-03-08 01:44:07

标签: jquery jquery-ui jquery-ui-tabs dynamic-linking

我不确定此刻是否可行,而且我所做的测试似乎提供了奇怪的结果。

我在一个页面上有一个包含4个标签的部分,这些标签内部是几个文本部分,每个部分都有一个唯一的锚名称。

我要做的是从另一页链接到第3栏中的第4块内容......

选项卡都运行良好,如果我链接到第一个选项卡上的内容部分,它工作得很好..当我想要链接到第一个它变得棘手的选项卡时...

我试过了

<a href="http://example.com#tab-3#content-4" ></a>

但这根本不起作用

当我只使用

<a href="http://example.com#tab-3"></a>

我也看到过这个实现 - 但它似乎具有与使用上面的代码相同的功能,无论这是否在我的jquery调用中

$(function(){
  $('tabs').tabs();
  var hash = location.hash;
  $('tabs').tabs( "select" , hash );
});

选择第三个选项卡时,如果选择以上两个选项之一,我会一直推到页面底部。我假设这是因为所有选项卡都放在列表项中,然后jqueryui将它们变成选项卡..实际上将数字3的选项卡内容从底部返回到选项卡部分的顶部..

如果有人知道如何链接到第3个标签上的第4个内容块,我会非常感激。

有人解决方案可能在$ _post和$ _get数据..但我不确定是否真的如此,即便如此我也不确定如何用jqueryui实现它

提前谢谢

4 个答案:

答案 0 :(得分:7)

试试这个:

作为链接使用

<a href="http://example.com#content-4" ></a>

脚本

    $(function(){
        $tabs = $('#tabs').tabs();

        var hash = location.hash.substring(1),

        $anchor = $tabs
            .find('a[name="' + hash + '"]'),

        tabId = $anchor.closest('.ui-tabs-panel')
            .attr('id');

        $tabs.find('ul:first a').each(
            function(i){
                    if($(this).attr('href') === '#' + tabId){
                        $tabs.tabs('select', i);
                        window.scrollTo(0, $anchor.position().top);
                        // Stop searching if we found it
                        return false;
                    }
            }
        );
    });

答案 1 :(得分:1)

根据您正在使用的服务器端脚本语言(如果有),您可以在网址$_GET[]中传递要选择的标签,然后将其作为selected选项回显您对tabs();小部件的调用。然后使用我在另一个堆栈线程中找到的漂亮函数,您可以获取所选元素的坐标并滚动到它。

e.g。使用PHP

<!--- link on another page -->
<a href="yourtabspage.php?tab=2&ctntid=content4">Your Link to Fourth Piece of Content in Second Tab</a>

然后在标签页上:

<?php 
  $your_tab = $_GET['tab']; 
  $your_selected_element = $_GET['ctntid'];
?>

<script type="text/javascript">

// this function is from another post on stackoverflow, I didn't write it.

function getOffset( el ) {
  var _x = 0;
  var _y = 0;
  while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
    _x += el.offsetLeft - el.scrollLeft;
    _y += el.offsetTop - el.scrollTop;
    el = el.offsetParent;
  }
  return { top: _y, left: _x };
}

jQuery(document).ready(function () {
  $("#tabs").tabs({
    selected: "<?php echo $your_tab; ?>"
  });


});

jQuery(window).load(function() {
  var x = getOffset( document.getElementById('<?php echo $your_selected_element;?>') ).left;
  var y = getOffset( document.getElementById('<?php echo $your_selected_element;?>') ).top;
  window.scrollTo(x,y)
});

</script>

答案 2 :(得分:0)

老问题,但今天我遇到了这个深层链接的jQuery标签需求。我还必须在帖子后面保留哈希值。下面的工作方式是深入链接到jQuery选项卡,并通过将哈希附加到表单的操作来保留回发的哈希。

    $(document).ready(function ($) {

        //Initialize the jQuery tabs
        $("#tabs").tabs({
            activate: function (event, ui) {
                //Add this ID to the URL, and scroll back to the top
                location.hash = ui.newPanel.attr('id');
                window.scrollTo(0, 0);

                //Update the form to append the hash to the action, for post-backs
                var newAction = $("#frmMain").attr("action").split('#')[0] + location.hash;
                $("#frmMain").attr("action", newAction);
            }
        });

        //When the page loads, if we have a hash, load that tab
        var hash = location.hash;
        if (hash.length > 0) {
            //Load the tab associated with this hash
            $("#tabs").tabs("load", hash);

            //Re-update the form to append the hash to the action, for post-backs
            var newAction = $("#frmMain").attr("action").split('#')[0] + hash;
            $("#frmMain").attr("action", newAction);
        }

    });

答案 3 :(得分:-1)

尝试

<a href="#" onclick="changetab(3)">Tab 4</a>


function changetab(idx)
{
  $('tabs').tabs( "select" , idx);
}