点击关闭标签

时间:2018-04-07 09:54:32

标签: javascript jquery html html5 css3

我正在尝试在我的网站上制作一个标签功能,我想让这些标签响应。所以我做了这样的代码:



function openCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    evt.currentTarget.className += " active";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
&#13;
/* Style the tab */
.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-top: none;
}

/* Style the close button */
.topright {
    float: right;
    cursor: pointer;
    font-size: 28px;
}

.topright:hover {color: red;}
&#13;
<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
  <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
  <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <span onclick="this.parentElement.style.display='none'" class="topright">&times</span>
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>
&#13;
&#13;
&#13;

现在我有两件事,

  • 首先是我想要点击标题关闭标题(伦敦, 巴黎,东京),然后从那个标题中删除活跃的类 点击它。

  • 其次,我想通过两个标签对此进行响应 行,第三行进入第二行,然后点击向右打开 单击下方的标签,然后按下第三个标签。

任何提示都会有所帮助,提前谢谢

1 个答案:

答案 0 :(得分:1)

你的问题没有。 1,您可以通过检查当前城市是否显示来从“openCity”更改为“toggleCity” - &gt;然后你隐藏,否则你会显示它。

function toggleCity(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
   
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    var prev_state_display = document.getElementById(cityName).style.display;
    
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    
    if( prev_state_display === "none"){
        document.getElementById(cityName).style.display = "block";
        evt.currentTarget.className += " active";
    } else {
        document.getElementById(cityName).style.display = "none";
        evt.currentTarget.className.replace(" active", "");
    }
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
/* Style the tab */
.tab {
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
    background-color: inherit;
    float: left;
    border: none;
    outline: none;
    cursor: pointer;
    padding: 14px 16px;
    transition: 0.3s;
    font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
    background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
    background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-top: none;
}

/* Style the close button */
.topright {
    float: right;
    cursor: pointer;
    font-size: 28px;
}

.topright:hover {color: red;}
<div class="tab">
  <button class="tablinks" onclick="toggleCity(event, 'London')" id="defaultOpen">London</button>
  <button class="tablinks" onclick="toggleCity(event, 'Paris')">Paris</button>
  <button class="tablinks" onclick="toggleCity(event, 'Tokyo')">Tokyo</button>
</div>

<div id="London" class="tabcontent">
  <span onclick="toggleCity(event, 'London')" class="topright">&times</span>
  <h3>London</h3>
  <p>London is the capital city of England.</p>
</div>

<div id="Paris" class="tabcontent">
  <span onclick="toggleCity(event, 'Paris')" class="topright">&times</span>
  <h3>Paris</h3>
  <p>Paris is the capital of France.</p> 
</div>

<div id="Tokyo" class="tabcontent">
  <span onclick="toggleCity(event, 'Tokyo')" class="topright">&times</span>
  <h3>Tokyo</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

关注你的问题没关系。 2,在这里你可以看到一个用“坏代码”写的样本,只是为了让你走上正轨。如果您调整iframe容器宽度的大小,它的行为就像您要求的那样..

虽然需要一个好的评论。 LINK

代码分为两部分,但在浮动的帮助下显示为单个。 检查CSS代码底部的@media

PS我使用了一些jquery代码,因为我在toggleCity函数没有被定义时遇到了一些错误。

jQuery方式

以下是使用jQuery的方法。只需添加CSS样式,如下所示

.tabcontent.active {display: block}

同时将HTML5 data属性添加到.tablinks,这是脚本

$('.tablinks').on('click', function(){
  let target_id = $(this).data('target');
  $('.tablinks, .tabcontent').removeClass('active');
  $(this).addClass('active');
  $(target_id).addClass('active');
});
$('.close-tab').on('click', function(){
  $('.tablinks, .tabcontent').removeClass('active');
});

$("#defaultOpen").click();

- &GT; Working Fiddle