【问题标题】:jQuery: Contains Vs EqualsjQuery:包含与等于
【发布时间】:2014-11-18 15:27:28
【问题描述】:

实际上,我确信以前(某处)曾有人问过这个问题,但我似乎无法以某种方式得到“正常”的答案。

我正在尝试使用 telerik 选项卡控件,并根据单击的链接文本选择打开选项卡。 页面总体布局:

+----------------------------------------+
|      HEADER                            |
+----------------------------------------+
| N  |+--------------------------------+ |
| A  ||    Tabstrip                    | |
| V  ||                                | |
| B  ||                                | |
| A  ||                                | |
| R  ||                                | |
|    ||                                | |
|    ||                                | |
|    |+--------------------------------+ |
+----+-----------------------------------+

即我的导航可能如下所示:

+-------------------------------------------------------------------------------------
|
|     HEADER                   
|                        
+------------------------+------------------------------------------------------------
| Users                  |    __________    __________    ___________
+------------------------+   /  Users   \  / Products \  / Suppliers \
| Products               |  /            \/____________\/_____________\_______________
+------------------------+ |
| Suppliers              | | Tab content here for users page
+------------------------+ |
| Orders                 | |
+------------------------+ |
| Stock                  | |
+------------------------+ |

所以,通过点击导航栏中的项目,我希望使相应的选项卡变为活动状态。

我的导航项是使用类似于以下内容的内容创建的:

<li id="users">    <a href="#">Users      </a></li>
<li id="products"> <a href="#">Products   </a></li>
<li id="suppliers"><a href="#">Suppliers  </a></li>
<li id="orders">   <a href="#">Orders     </a></li>
<li id="stock">    <a href="#">Stock      </a></li>

然后我有一个选项卡控件(它是自动生成的),并且页面检查器在运行时显示

这是实际的选项卡控件。它们是通过以下方式制成的:

@(Html.Kendo().TabStrip()
  .Name("tabMain")
  .Animation(true)

  .Items(items =>
      {
        /*index 0 */  items.Add().Encoded(false).ImageUrl("~/Content/Images/CLOSE.png").Text("Users&nbsp&nbsp&nbsp&nbsp").Content(Html.Action("Index", "User").ToString());
        /*index 1 */  items.Add().Encoded(false).ImageUrl("~/Content/Images/CLOSE.png").Text("Products   &nbsp&nbsp&nbsp&nbsp").Content(Html.Action("Index", "Products).ToString());
        /*index 2 */  items.Add().Encoded(false).ImageUrl("~/Content/Images/CLOSE.png").Text("Suppliers&nbsp&nbsp&nbsp&nbsp").Content(Html.Action("Index", "Suppliers").ToString());
        /*index 3 */  items.Add().Encoded(false).Text("Orders").Content(Html.Action("Index", "Orders").ToString());
        /*index 4 */  items.Add().Encoded(false).Text("Stock").Visible(false).Content(Html.Action("Index", "Stock").ToString());

      })
 )

实际问题

我希望能够按下导航栏上的按钮并显示指定的选项卡/使其处于活动状态。

所以,足够的背景,我目前正在使用 jquery:

 $('#stock').click(function(e){
            //alert("Stock is what was pressed");
            $('#tabMain-1').toggle(); //simply used for testing purposes
            $("");
 });

尝试选择相应的选项卡。

我的选择器应该是什么来获得唯一的选项卡 id/index/aria-controls(带有值 - 这似乎是他们的“控件的唯一 id”版本)?我应该使用“:Equals”还是“:Contains”?

或者我应该使用完全不同的东西来选择这个标签标题名称吗?

Html,根据要求:

【问题讨论】:

  • #stock 突然从哪里来的?
  • 抱歉,我没有在导航栏中显示所有链接。现在将添加
  • 你能提供截图中显示的 HTML 吗?另外,应该根据什么选择&lt;div&gt;?在它的索引上?
  • @Regent 添加(对于它的价值) - 至于你的最后一点,我想我可以选择这个 k-item 如果我可以检查它是否有一个 aria-controls 属性以及tabMain-...的属性值
  • @jbutler483 如果您不想添加与.kendoToolBar() JS 和 CSS 相关的小提琴以允许查看最终(在 Kendo 修改 DOM 之后)HTML - 这是您的选择。关于aria-controls的定位元素:fiddle example

标签: jquery html telerik


【解决方案1】:

这是一个艰难的过程。我不熟悉剑道控件,但根据您的标记,我认为下面的代码可能会起作用。仅当您的导航项与选项卡项的顺序完全相同时,该代码才有效,即:下面的代码使用基于索引的搜索。

    $('.nav li').click(function(){

        //get the index of the nav item that was clicked
        var index = $('.nav li').index(this);

        //remove the active css class from all of the tabs
        $('.k-tabstrip-items .k-item').removeClass('k-state-active');

        //get the tab at the specific index
        var tabItem = $($('.k-tabstrip-items .k-item').get(index));

        //add the active css class to the tab
        tabItem.addClass('k-state-active');

        //get the tab id from the tab item
        var newTabId = tabItem.attr('aria-controls');

        //remove the active css class from all of the tab content divs
        $('.k-content')
            .removeClass('k-state-active')
            .attr('aria-expanded', false)
            .attr('aria-hidden', true);

        //add the active css and set the aria properties for the selected tab content div
        $('#' + newTabId)
            .addClass('k-state-active')
            .attr('aria-hidden', false)
            .attr('aria-expanded', true);

    });

注意:此代码假定您的&lt;ul&gt; 标记具有nav 类,即:&lt;ul class="nav"&gt;。如果没有,您需要稍微更改一下 javascript。

【讨论】:

  • $($('.k-tabstrip-items .k-item').get(index)) 可以简单地为$('.k-tabstrip-items .k-item').eq(index)
  • 不错!我不知道。
猜你喜欢
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 2018-09-17
  • 2017-09-05
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多