在jquery函数中创建选项卡并向其参数添加内容

时间:2017-07-18 05:46:55

标签: jquery html

我遇到了挑战,我想制作标签页

这是我的Tab键代码:

<ul>
<li class="Tabs Active" id="Tab1" onclick="SetActive(Tab1)">
   <div>
        <span>Tab1</span>
   </div>
</li>
<li class="Tabs" id="Tab2" onclick="SetActive(Tab2)">
   <div>
        <span>Tab2</span>
   </div>
</li>

这是标签页:

<div class="BodyBlock">
 <div class="Content">
     <div class="TabData" id="DataTab1">
        Tab1 Data
     </div>
     <div class="TabHidden TabData" id="DataTab2">
         Tab2 Data
     </div>
 </div>

它是显示用户点击Tab按钮

的页面的功能
function SetActive(id) {
    $('.Tabs').removeClass('Active');
    $(id).addClass('Active');
    $('.TabData').hide();
    $('Data'+id).show();
}

当我想添加“数据”时,我的问题出现了。在第4行的SetActive函数中添加标签按钮时无法正常工作且标签页不显示 我使用jquery v3.2.1

3 个答案:

答案 0 :(得分:1)

您需要为这2行$(id).addClass('Active')$('Data'+id).show();

添加.Tabs.Active { color:red;}

看一下下面的例子,我让你的代码工作

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="Tabs Active" id="Tab1" onclick="SetActive('Tab1')">
   <div>
        <span>Tab1</span>
   </div>
</li>
<li class="Tabs" id="Tab2" onclick="SetActive('Tab2');">
   <div>
        <span>Tab2</span>
   </div>
</li>

<div class="BodyBlock">
 <div class="Content">
     <div class="TabData" id="DataTab1">
        Tab1 Data
     </div>
     <div class="TabHidden TabData" id="DataTab2">
         Tab2 Data
     </div>
 </div>

<script>
function SetActive(id) {
    $('.Tabs').removeClass('Active');
    $("#" + id).addClass('Active');
    $('.TabData').hide();
    $('#Data'+id).show();
}
</script>
{{1}}

答案 1 :(得分:0)

您需要纠正第4行:

$('#Data'+id).show();

答案 2 :(得分:0)

你在最后一行忘了。请参阅以下代码

<li class="Tabs" id="Tab2" onclick="SetActive('Tab1')">

<li class="Tabs" id="Tab2" onclick="SetActive('Tab2')">

您还会忘记单引号 SetActive('Tab1') SetActive('Tab2')

var myStr = "<div><span>Hello<span><div>SearchString<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div></div></div>";