简体   繁体   English

如何设置电流<li> JQuery 中的选项卡

[英]How can I set the current <li> tab in JQuery

So, originally I have this html code which I'm trying to implement the <li> 's to be dynamically added in the <ul>所以,最初我有这个 html 代码,我试图实现<li>的动态添加到<ul>

<ul class="tabs" id="modal_addquestion_ul">
    <li class="tab-link current" data-tab="multipleChoice">Multiple Choice</li>
    <li class="tab-link" data-tab="trueOrFalse">True or False</li>
    <li class="tab-link" data-tab="fillInTheBox">Fill in the Box</li>
</ul>

The current tab is set to Multiple Choice.当前选项卡设置为多项选择。

Now, I create a method in JQuery to dynamically add the <li> 's to the <ul> Data is from the database I created.现在,我在 JQuery 中创建了一个方法来将<li>动态添加到<ul>数据来自我创建的数据库。

function loadQuestionTypesToULTab(ULId){
    $.ajax({
        url: 'controller/get_all_question_types.php',
        type: 'POST',
        dataType: 'json',
        success: function (questionTypeData) {
            console.log(questionTypeData);
            var len = questionTypeData.length;
            clearListItemsOf(ULId);
            for (var i = 0; i < len; i++) {
                var questionTypeId = questionTypeData[i]['questionTypeId'];
                var questionType = questionTypeData[i]['questionType'];
                var listItem = $('<li></li>').val(questionTypeId)
                                .text(questionType).addClass('tab-link').attr('data-tab',questionType);
                $(ULId).append(listItem);
                //$(ULId).append("<li class='tab-link'+' current' value='" + questionTypeId + "' >"    + questionType +    "</li>" );
            }
        },
        error: function (x, e) {
            handleError(x, e);
        }
    });
}

When I used Google Chrome's inspector to see if JQuery method translates exactly of how the original html above was written, it's almost correct except that it's not setting Multiple Choice as current <li> or tab.当我使用 Google Chrome 的检查器查看 JQuery 方法是否准确翻译了上面原始 html 的编写方式时,它几乎是正确的,只是它没有将Multiple Choice设置当前<li>或选项卡。

Output in Google Chrome's inspector when <li> 's were inspected:检查<li>时 Google Chrome 检查器中的输出:

<li value="1" class="tab-link" data-tab="Multiple Choice">Multiple Choice</li>
<li value="2" class="tab-link" data-tab="Fill in the Box">Fill in the Box</li>
<li value="3" class="tab-link" data-tab="True or False">True or False</li>

What do I need to add to my JQuery code to produce,我需要添加到我的 JQuery 代码中以生成什么,

Multiple Choice多项选择

Notice that the class value is tab-link current请注意,类值是当前的 tab-link

I tried .addClass('tab-link current');我试过.addClass('tab-link current'); but only takes tab-link and drops current in html.但只需要tab-link并在 html 中删除当前

As in,就像在,

<li value="1" class="tab-link" data-tab="Multiple Choice">Multiple Choice</li>

Without current as part of the name class.没有current作为名称类的一部分。

Thank you.谢谢你。

Like you said, you do need to also specifically add the class 'current' which is not in your main code sample.就像您说的那样,您还需要专门添加不在主代码示例中的“当前”类。

You said you tried addClass('tab-link current') but we can't see that in the context of your whole code.你说你试过addClass('tab-link current')但我们在你的整个代码的上下文中看不到。 You have a commented out line, but it doesn't make sense by itself because it would apply to all of the items and seems to be from an earlier iteration of your code.您有一条注释掉的行,但它本身没有意义,因为它适用于所有项目,并且似乎来自代码的早期迭代。

How is your code to know which item should get the current class, if any?您的代码如何知道哪个项目应该获得当前类(如果有)? Perhaps try something like this (setting the first item to current, in this case):也许尝试这样的事情(在这种情况下,将第一项设置为当前):

success: function (questionTypeData) {
        var len = questionTypeData.length;
        clearListItemsOf(ULId);
        for (var i = 0; i < len; i++) {
            var questionTypeId = questionTypeData[i]['questionTypeId'];
            var questionType = questionTypeData[i]['questionType'];
            var listItem = $('<li></li>').val(questionTypeId)
                            .text(questionType).addClass('tab-link').attr('data-tab',questionType);
            if ( i == 0 ){
                listItem.addClass( 'current' );
            }
            $(ULId).append(listItem);
        }
    },

Try .addClass('tab-link');试试 .addClass('tab-link'); and then .addClass('current');然后 .addClass('current'); This should work.这应该有效。 Or .addClass('tab-link').addClass('current');或者 .addClass('tab-link').addClass('current');

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

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