【问题标题】:How to select a jQuery tab from C# code behind?如何从后面的 C# 代码中选择一个 jQuery 选项卡?
【发布时间】:2014-03-17 06:07:35
【问题描述】:

我正在使用 jQuery 选项卡式内容。

<div class="container">
    <ul class="tabs">
        <li>
            <a id= "tab1" runat="server" href="#tab1" class="active" >Tab1</a>
        </li>
        <li>
            <a id= "tab2" runat="server" href="#tab2" >Tab2</a>
        </li>
           
    </ul>
    <div id="div1" class="form-action show">
    ........
    </div>

    <div id="div2" class="form-action hide">
    .......
    </div>      
</div>

jQuery 函数

(function ($) {
    // constants
    var SHOW_CLASS = 'show',
  HIDE_CLASS = 'hide',
  ACTIVE_CLASS = 'active';
  
    $('.tabs').on('click', 'li a', function (e) {
        e.preventDefault();
        var $tab = $(this),
     href = $tab.attr('href');

        $('.active').removeClass(ACTIVE_CLASS);
        $tab.addClass(ACTIVE_CLASS);

        $('.show')
    .removeClass(SHOW_CLASS)
    .addClass(HIDE_CLASS)
    .hide();

        $(href)
    .removeClass(HIDE_CLASS)
    .addClass(SHOW_CLASS)
    .hide()
    .fadeIn(550);
    });
})(jQuery);

标签工作正常。当从另一个页面请求一个页面时,我想要根据查询字符串值选择的选项卡。例如,如果我通过了

<a href="Page.aspx?tab=tab1">Tab1</a>

然后应该选择 Tab1。如果我通过了

<a href="Page.aspx?tab=tab2">Tab2</a>

那么应该选择 Tab2。

【问题讨论】:

标签: javascript c# jquery asp.net


【解决方案1】:

如果您使用的是 jQuery UI,只需使用 hashtag 并指向选项卡 ID; jQuery 将完成剩下的工作:

<a href="Page.aspx#tab2">Tab2</a>

如果您不使用 jQuery UI,请按照以下说明操作:

使用hashchange 事件更好地定义单击哪个选项卡。我对您的代码进行了一些修改,请检查一下。

(function ($) {
    // constants
    var SHOW_CLASS = 'show',
  HIDE_CLASS = 'hide',
  ACTIVE_CLASS = 'active';

    $(window).on('hashchange', function() {
        href = window.location.hash;
        if (href == "") return;
        $('.tabs li a').removeClass('active');
        $('.tabs li a[href=' + href + ']').addClass('active');
        
        $('.show')
    .removeClass(SHOW_CLASS)
    .addClass(HIDE_CLASS)
    .hide();

        $(href)
    .removeClass(HIDE_CLASS)
    .addClass(SHOW_CLASS)
    .hide()
    .fadeIn(550);
    });
    $(window).trigger('hashchange'); // If the page is loaded from another page
})(jQuery);

Working demo

【讨论】:

  • 你能发布你的问题的现场演示吗?
  • 是的,如果链接在同一页面中,这是有效的。如果我使用查询字符串从另一个页面传递 #tab1 或 #tab2 ,那么它没有选择所需的选项卡。
  • @DiwakarChaudhary:只需要在页面加载时触发hashchange事件即可。请参阅我的更新答案。
【解决方案2】:

HTML:

<asp:HiddenField runat="server" ID="hdn" />

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["tab"] != null)
            {
                hdn.Value = Request.QueryString["tab"].ToString();
            }
        }
    }

CSS:

<style type="text/css">

.active {
    background:Green;
}
</style>

jQuery:

<script type="text/javascript">

    $(document).ready(function () {
        // constants
        var SHOW_CLASS = 'show',
        HIDE_CLASS = 'hide',
        ACTIVE_CLASS = 'active';
        $('.tabs').tabs();
          
        var class_1 = $('#<%= hdn.ClientID %>').val();
        $('.tabs > li').find('a').removeClass('active');
        $('.tabs > li').each(function (index) {
            if ($(this).find('a').attr('id') == class_1) {
                $(this).find('a').addClass('active');  
            }
        });

        $('.tabs').on('click', 'li a', function (e) {
            e.preventDefault();
            var $tab = $(this),
            href = $tab.attr('href');

            $('.active').removeClass(ACTIVE_CLASS);
            $tab.addClass(ACTIVE_CLASS);

            $('.show')
            .removeClass(SHOW_CLASS)
            .addClass(HIDE_CLASS)
            .hide();

            $(href)
           .removeClass(HIDE_CLASS)
           .addClass(SHOW_CLASS)
           .hide()
           .fadeIn(550);
        });
    });
</script>

【讨论】:

    【解决方案3】:

    你必须做两件事:

    1. 检测查询字符串
    2. 触发“标签”以触发(就像用户单击它一样)。

    1) 在您的 Page.aspx.cs 中

    protected void Page_Load(object sender, EventArgs e)
    {
        //Get the query string called tab
        private string tab = Request.Querystring("tab");
        //Check that query string is not null
    
        if(tab!=null)
        {
           //Run JavaScript. NB: the parameter passed to this is based off our query string
           ScriptManager.RegisterStartupScript(this, typeof(string), "Registering", String.Format("openTab('{0}');", tab), true);
        }
        
    }
    

    2) 在您的 JavaScript 中,只需创建一个新方法

    function openTab(tab) {
        //Will fake a user "click" for the tab that aSP.NET told it to open
        $(".tabs li a").each(function(){
            var id = $(this).attr("href").replace("#", "");
            if(id==tab) $(this).trigger("click");
        });
    }
    

    抱歉,我没有测试过,请尝试一下!

    这里的关键是 ASP.NET 提供的 ScriptManager。它使您可以与客户端脚本进行通信,因此您可以使用它来做您需要做的事情

    编辑:我更新了openTab()的JavaScript函数。您可以在此处看到一个示例小提琴在加载时打开第二个选项卡

    http://jsfiddle.net/y8Wuw/10/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多