简体   繁体   English

添加和删​​除类元素子级

[英]Add and remove class element children

I have this checkmark block. 我有此选中标记块。 As the name says, it draws a check mark similar to the one we have here, on Stackoverflow. 顾名思义,它在Stackoverflow上绘制了一个类似于此处的复选标记。 I want both of those elements (checkmark_stem and checkmark_kick) turn green when they're active, so I tried to do this: 我希望这两个元素(checkmark_stem和checkmark_kick)都处于活动状态时变为绿色,所以我尝试这样做:

window.setInterval(function () {
        $('body').find('.checkmark').each(function (index) {
            var current = $(this);
            console.log(current);
            var url = current.data('refresh');
            console.log(url);
            var child = current.children;
            var answerID = current.data('answer');
            console.log(child);
            $.get(url + '?id=' + answerID, function (data) {

                data = JSON.parse(data);

                if(data.rating.solved_date === null)
                    current.children[0].removeClass('active');
                else
                    current.children[1].addClass('active');



            });
        });
    }, 1000);

but it seems I'm not doing this correctly. 但似乎我没有正确执行此操作。 Is there any way to addClass('active') to both elements? 有什么办法可以将addClass('active')到两个元素?

My element html 我的元素html

<blockquote class="accept-answer text-right {if !$isMine} hidden{/if}" >
            <div class="checkmark" title="Accept this answer" data-url="{url('controller/api/questions/mark_as_solved')}" data-refresh="{url('controller/api/questions/refresh_accepted_answers')}" data-answer="{$answer['answerid']}" data-question="{$question['publicationid']}">
                <div class="checkmark_kick" title="Unnacept this answer"></div>
                <div class="checkmark_stem" title="Unnacept this answer"></div>
            </div>
        </blockquote>

EDIT: I had like this before and it worked. 编辑:我以前曾经这样,并且它起作用。 I just wanted the mark to look more like a check 我只是想让商标看起来更像是支票

<blockquote class="accept-answer text-right {if !$isMine} hidden{/if}" >
            <div class="accept" title="Accept this answer" data-url="{url('controller/api/questions/mark_as_solved')}" data-refresh="{url('controller/api/questions/refresh_accepted_answers')}" data-answer="{$answer['answerid']}" data-question="{$question['publicationid']}">
                <div class="accepted up" title="Unnacept this answer"></div>
            </div>
        </blockquote>



window.setInterval(function () {
        $('body').find('.accepted.up').each(function (index) {
            var current = $(this);
            console.log(current);
            var url = current.parent().data('refresh');
            console.log(url);
            var parent = current.parent();
            var answerID = parent.data('answer');
            console.log(answerID);
            $.get(url + '?id=' + answerID, function (data) {

                data = JSON.parse(data);

                if(data.rating.solved_date === null)
                    current.removeClass('active');
                else
                    current.addClass('active');



            });
        });
    }, 1000);

Kind regards 亲切的问候

Apologies if I am not understanding your question but give this a try: 抱歉,如果我不了解您的问题,请尝试一下:

    if(data.rating.solved_date === null) {
    for (var i = 0; i < current[0].children.length; i++) {
       current[0].children[i].classList.remove("active");
    }
    } else {

    for (var i = 0; i < current[0].children.length; i++) {
        current[0].children[i].classList.add("active");
        }

}   

Try this - it should find all the elements under current object. 试试这个-它应该找到当前对象下的所有元素。

 window.setInterval(function () {
    $('body').find('.checkmark').each(function (index) {
        var current = $(this);
        console.log(current);
        var url = current.data('refresh');
        console.log(url);
        var child = current.children;
        var answerID = current.data('answer');
        console.log(child);
        $.get(url + '?id=' + answerID, function (data) {

            data = JSON.parse(data);

            if (data.rating.solved_date == null)
               current.find('div').removeClass('active')

            else
              current.find('div').addClass('active')


        });
    });
}, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM