【问题标题】:Add and Remove class to click a dynamic Button添加和删​​除类以单击动态按钮
【发布时间】:2015-08-23 15:25:59
【问题描述】:

尝试添加和删除类以单击动态按钮,表示此按钮<button class="one"></button> 动态获取类,如下所示:<button class="one text1">text1</button>
因此,如果 按钮一 具有类 .text1 并通过 单击 将此添加类 .hide 到列表项 <li class="text1"> 就像 <li class="text1 show">
按钮二 <button class="two"></button> 和单击添加类 <li class="text2 show">

相同

注意:当单击按钮二时,应删除类.show并添加新类.hide按钮一

主 HTML:

<div id="main-id">
    <button class="one"></button>
    <button class="two"></button>
    <ul>
        <li>
            <!--List 1-->
            <div class="label">
                <a href="#">text1</a>
            </div>
        </li>
        <li>
            <!--List 2 is Same-->
            <div class="label">
                <a href="#">text1</a>
            </div>
        </li>
        <li>
            <!--List 3 is different-->
            <div class="label">
                <a href="#">text2</a>
            </div>
        </li>
    </ul>
</div>

脚本:

$('.label a').each(function() {
   var $this=$(this);      
   $this.closest('li').addClass($this.text());
});

// Combine This

$('button').each(function(){
    var liInd = 0;
    var cl = '';
    var txt = '';
    var clses = [];

    var ind = $('button').index($(this)) + 1;

    $('li').each(function(){
        if(clses.indexOf($(this).attr('class')) === -1){
            clses.push($(this).attr('class'));
            liInd = liInd + 1;
        }

        if(ind === liInd){
            cl = $(this).attr('class');
            txt = $(this).find('a').text();
            return false; //break
        }
    });

    $('button:nth-child(' + ind + ')').addClass(cl);
    $('button:nth-child(' + ind + ')').text(txt);
});

Example on Fiddle

我已经尝试通过单击功能添加/删除类,但问题是按钮从列表项中动态获取类,所以我无法定位按钮。
JS/Jquery 对其他方法有什么建议吗?

【问题讨论】:

  • 你的问题不够清楚。
  • 我的意思是要添加/删除类以通过点击功能列出项目。
  • 检查这个DEMO

标签: javascript jquery html addclass removeclass


【解决方案1】:

这是另一种解决方案

$('button').each(function () {
    var liInd = 0;
    var cl = '';
    var txt = '';
    var clses = [];

    var ind = $('button').index($(this)) + 1;

    $('li').each(function () {
        if (clses.indexOf($(this).attr('class')) === -1) {
            clses.push($(this).attr('class'));
            liInd = liInd + 1;
        }

        if (ind === liInd) {
            cl = $(this).attr('class');
            txt = $(this).find('a').text();
            return false; //break
        }
    });

    if (txt != '') {
        $('button:nth-child(' + ind + ')').addClass(cl);
        $('button:nth-child(' + ind + ')').text(txt);
    }
});

$('button').click(function () {
    if ($(this).attr('class')[0] == 'all') {
        showAll();
        return false; // end this function
    }

    var allCls = $(this).attr('class').split(' ');



    $('li').each(function () {

        if (allCls.indexOf($(this).find('a').text()) > -1) {
            $(this).closest('li').removeClass('show').addClass('hide');
        } else {
            $(this).closest('li').removeClass('hide').addClass('show');
        }
    });
});

function showAll() {
    $('li').removeClass('hide').addClass('show');
}

小提琴:https://jsfiddle.net/taleebanwar/yaLm4euk/13/

【讨论】:

  • 嗨 Taleeb,谢谢,我已将文本 ALL 添加到 &lt;button class="all"&gt;All&lt;/button&gt;,但此文本已从 &lt;button class="all"&gt;&lt;/button&gt; 中删除!请解决这个问题。
  • 但此文本 ALL 正在从 中删除!请解决这个问题。
  • 我的意思是新文本 ALL&lt;button class="all"&gt; All&lt;/button&gt; 中删除。我已将所有文本添加到 ,请参阅:jsfiddle.net/yaLm4euk/15
  • 是的 - 代码应该可以在 IE9 上运行(但 jsFiddle 可能不会)
【解决方案2】:

DEMO

$('.label a').each(function () {
    var $this = $(this);
    $this.closest('li').addClass($this.text());
});

// Combine This
$('button').each(function () {
    var liInd = 0;
    var cl = '';
    var txt = '';
    var clses = [];
    var ind = $('button').index($(this)) + 1;
    $('li').each(function () {
        if (clses.indexOf($(this).attr('class')) === -1) {
            clses.push($(this).attr('class'));
            liInd = liInd + 1;
        }
        if (ind === liInd) {
            cl = $(this).attr('class');
            txt = $(this).find('a').text();
            return false; //break
        }
    });
    $('button:nth-child(' + ind + ')').addClass(cl);
    $('button:nth-child(' + ind + ')').text(txt);
});
$(document).on('click', 'button',function(e){
    var textClass = $.grep(this.className.split(" "), function(v, i){
       return v.indexOf('text') === 0;
    }).join();
    console.log(textClass);
    $('li').removeClass('show').addClass('hide')
    $('li').each(function(){
    	if($(this).hasClass($.trim(textClass))){
        	$(this).removeClass('hide').addClass('show');
        } else {
            $(this).removeClass('show').addClass('hide');
        }
    })
})
.show{display:list-item;}
.hide{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="main-id">
        <button class="one"></button>
        <button class="two"></button>
        <ul>
            <li>
                <!--List 1-->
                <div class="label">
    <a href="#">text1</a>

                </div>
            </li>
            <li>
                <!--List 2 is Same-->
                <div class="label">
    <a href="#">text1</a>

                </div>
            </li>
            <li>
                <!--List 3 is different-->
                <div class="label">
    <a href="#">text2</a>

                </div>
            </li>
        </ul>
    </div>

【讨论】:

  • 哇,它起作用了,还有一点是,如果想通过单击新按钮名称all / &lt;button class="all"&gt; &lt;/button&gt; 来显示所有项目然后添加类.show-all ,那么它应该是什么?请看这个小提琴:jsfiddle.net/yaLm4euk/3
  • @Aabira 你可以做这样的事情。 DEMO 使用某些条件。
  • 谢谢,我已将文本 ALL 添加到 &lt;button class="all"&gt;All&lt;/button&gt;,但此文本已从 &lt;button class="all"&gt;&lt;/button&gt; 中删除!请解决这个问题。
  • @Aabira 在最后一个按钮的情况下,您的函数为变量 txt 返回 null。 FIXtxt = txt || 'all';
猜你喜欢
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 2019-04-30
  • 1970-01-01
相关资源
最近更新 更多