根据活动类获取数据属性的值 [英] fetch the value of data-attribute based on active class

查看:98
本文介绍了根据活动类获取数据属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于
ul > li class = "activeTab"

这是我的代码.

<ul class="tabs">
    <li data-itemtype="1" class="activeTab"><a href="#tab1">Published</a></li>
    <li data-itemtype="0" class=""><a href="#tab2">Unpublished</a></li>
</ul>

如果class="activeTab"在第一个<li>元素中,则获取第一个li元素的数据属性,即在这种情况下为1,因此对于第二个或任意数量的li元素同样适用.

if class="activeTab" is in first <li> element then fetch the data attribute of first li element i.e 1 in this case and hence same applies for the second or any number of li elements.

我尝试过

var itemType = $('ul.tabs').find('li.activeTab').attr('data-itemtype');

,它不起作用.它显示了未定义的值.

and it does not work. it shows value undefined.

更新:

我正在使用标签.我想知道哪个标签当前处于活动状态,我需要在javascript变量中使用该值.

i am using tabs. i want to know which tab is currently active and i need that value in javascript variable.

这是我的HTML代码.

here is my HTML code.

<div class="widget">
    <ul class="tabs">
        <li data-itemtype="1"><a href="#tab1">Published</a></li>
        <li data-itemtype="0"><a href="#tab2">Unpublished</a></li>
    </ul>
    <div class="tab_container">
        <div id="tab1" class="tab_content">
            <div class="widget" style="margin-top:0px;">
                <div class="title"><img src="/images/icons/dark/cart3.png" alt="" class="titleIcon" /><h6>Published Items</h6></div>                          
                <p>Content</p>
            </div>
        </div>
        <div id="tab2" class="tab_content"> 
            <div class="widget" style="margin-top:0px;">
                <div class="title"><img src="/images/icons/dark/cart3.png" alt="" class="titleIcon" /><h6>Unpublished Items</h6></div>
                <p>Content</p>
            </div>
        </div>
    </div>  
    <div class="clear"></div>        
</div>

正在使用的jQuery是这个.

the jQuery being used is this.

$.fn.contentTabs = function(){ 

        $(this).find(".tab_content").hide(); //Hide all content
        $(this).find("ul.tabs li:first").addClass("activeTab").show(); //Activate first tab
        $(this).find(".tab_content:first").show(); //Show first tab content

        $("ul.tabs li").click(function() {
            $(this).parent().parent().find("ul.tabs li").removeClass("activeTab"); //Remove any "active" class
            $(this).addClass("activeTab"); //Add "active" class to selected tab
            $(this).parent().parent().find(".tab_content").hide(); //Hide all tab content
            var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
            $(activeTab).show(); //Fade in the active content
            return false;
        });

    };
    $("div[class^='widget']").contentTabs(); //Run function on any div with class name of "Content Tabs"

上面的代码对我不起作用.

the above code does not work for me.

推荐答案

似乎工作正常.

我会改用.data(...):

var itemType = $('ul.tabs').find('li.activeTab').data('itemtype');
alert(itemType);

提琴: http://jsfiddle.net/maniator/2GYxh/

这篇关于根据活动类获取数据属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆