2017-03-29 1 views
0

2つのタブセクションがあり、それぞれのタブセクションの最初のタブをデフォルトで選択したいと思います。この場合、タブ1とタブ4。どうやってやるの?タブ1と4を選択する方法

私の試みでは、クラス名で選択したforループを作成しましたが、ループの最後の要素だけがすべてではなく常に選択されていることに気付きました。一度に1つのタブしか選択できないという問題があると思いますが、このステートメントがどこにあるのか分かりません。

1週間前にjsをピックアップしました。これは私がw3schoolsから得た変更された例です。

 <!DOCTYPE html> 
<html> 
<head> 
<style> 
body {font-family: "Lato", sans-serif;} 

/* Style the tab */ 
div.tab { 
    overflow: hidden; 
    border: 1px solid #ccc; 
    background-color: #f1f1f1; 
} 

/* Style the buttons inside the tab */ 
div.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 */ 
div.tab button:hover { 
    background-color: #ddd; 
} 

/* Create an active/current tablink class */ 
div.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: 20px; 
} 

.topright:hover {color: red;} 
</style> 
</head> 
<body> 

<p>Click on the x button in the top right corner to close the current tab:</p> 

<div class="tab"> 
    <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen1">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">x</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">x</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">x</span> 
    <h3>Tokyo</h3> 
    <p>Tokyo is the capital of Japan.</p> 
</div> 

<p>Click on the x button in the top right corner to close the current tab:</p> 

<div class="tab"> 
    <button class="tablinks" onclick="openCity(event, '1')" id="defaultOpen2">1</button> 
    <button class="tablinks" onclick="openCity(event, '2')">2</button> 
    <button class="tablinks" onclick="openCity(event, '3')">3</button> 
</div> 

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

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

<div id="3" class="tabcontent"> 
    <span onclick="this.parentElement.style.display='none'" class="topright">x</span> 
    <h3>Tokyo</h3> 
    <p>Tokyo is the capital of Japan.</p> 
</div> 

<script> 
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("defaultOpen1").click(); 
document.getElementById("defaultOpen2").click(); 
</script> 

</body> 
</html> 
+0

タブはありません4 ..最初のセクションには3つのタブがあり、2番目のセクションには3つのタブがあります。 –

+0

申し訳ありません。私はデフォルトで各セクションの最初のタブを開いていました。 – user3085351

答えて

0

問題は、タブのリンクをクリックしたときには、グローバルコールを作っているということなので、Javascriptをクラスに対してtabContentを探して起動し、そうでない場合でも、それらのすべてを隠しますこの問題を解決するには、非表示にしたいタブをクリックしてタブのコンテナのIDを見つけ、次に表示して非表示にする適切なコンテンツを見つけます。私はコードでいくつか微調整を行いました。

注:これは解決策ですが、確かにこのタブ

function openCity(evt, cityName) { 
 
    var i, tabcontent, tablinks; 
 
    var parent = evt.target.parentNode.parentNode.className; 
 
    console.log(parent); 
 
    tabcontent = document.querySelectorAll("." + parent + " .tabcontent"); 
 
    for (i = 0; i < tabcontent.length; i++) { 
 
     tabcontent[i].style.display = "none"; 
 
    } 
 
    tablinks = document.querySelectorAll("." + parent + " .tablinks"); 
 
    for (i = 0; i < tablinks.length; i++) { 
 
     tablinks[i].className = tablinks[i].className.replace(" active", ""); 
 
    } 
 
// document.getElementById(cityName).style.display = "block"; 
 

 

 
    document.querySelector("." + parent +" #" + cityName).style.display = "block"; 
 
// console.log(this) 
 
    evt.currentTarget.className += " active"; 
 
} 
 

 
// Get the element with id="defaultOpen" and click on it 
 
document.getElementById("defaultOpen1").click(); 
 
document.getElementById("defaultOpen2").click();
body {font-family: "Lato", sans-serif;} 
 

 
/* Style the tab */ 
 
div.tab { 
 
    overflow: hidden; 
 
    border: 1px solid #ccc; 
 
    background-color: #f1f1f1; 
 
} 
 

 
/* Style the buttons inside the tab */ 
 
div.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 */ 
 
div.tab button:hover { 
 
    background-color: #ddd; 
 
} 
 

 
/* Create an active/current tablink class */ 
 
div.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: 20px; 
 
} 
 

 
.topright:hover {color: red;}
 <!DOCTYPE html> 
 
<html> 
 
<head> 
 
</head> 
 
<body> 
 

 
<p>Click on the x button in the top right corner to close the current tab:</p> 
 
<div class="cities"> 
 
    <div class="tab"> 
 
    <button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen1">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">x</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">x</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">x</span> 
 
    <h3>Tokyo</h3> 
 
    <p>Tokyo is the capital of Japan.</p> 
 
    </div> 
 
</div> 
 

 
<p>Click on the x button in the top right corner to close the current tab:</p> 
 

 
<div class="countries"> 
 
    <div class="tab"> 
 
    <button class="tablinks" onclick="openCity(event, 'one')" id="defaultOpen2">1</button> 
 
    <button class="tablinks" onclick="openCity(event, 'two')">2</button> 
 
    <button class="tablinks" onclick="openCity(event, 'three')">3</button> 
 
    </div> 
 

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

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

 
    <div id="three" class="tabcontent"> 
 
    <span onclick="this.parentElement.style.display='none'" class="topright">x</span> 
 
    <h3>Tokyo</h3> 
 
    <p>Tokyo is the capital of Japan.</p> 
 
    </div> 
 
</div> 
 

 
</body> 
 
</html>

+0

カルロスこれは非常に良い解決策です。ありがとうございました。 – user3085351

0

あなたが何を意味していたかわかりません。しかし、ここで働くフィドルです。

あなたのタブにコンテナDIVを追加して、javascriptで最初の要素を取得しやすくなるよう助言します。

他に何か必要がある場合は教えてください。

window.openCity = function (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"; 
 
} 
 

 

 
document.getElementById("London").style.display='block'; 
 
document.getElementById("1").style.display='block';
body {font-family: "Lato", sans-serif;} 
 

 
/* Style the tab */ 
 
div.tab { 
 
    overflow: hidden; 
 
    border: 1px solid #ccc; 
 
    background-color: #f1f1f1; 
 
} 
 

 
/* Style the buttons inside the tab */ 
 
div.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 */ 
 
div.tab button:hover, 
 
div.tab button.active{ 
 
    background-color: #ddd; 
 
} 
 

 
/* Create an active/current tablink class */ 
 
div.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: 20px; 
 
} 
 

 
.topright:hover {color: red;}
<p>Click on the x button in the top right corner to close the current tab:</p> 
 

 
<div class="tab"> 
 
    <button class="tablinks active" onclick="openCity(this, 'London');" id="defaultOpen1">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">x</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">x</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">x</span> 
 
    <h3>Tokyo</h3> 
 
    <p>Tokyo is the capital of Japan.</p> 
 
</div> 
 

 
<p>Click on the x button in the top right corner to close the current tab:</p> 
 

 
<div class="tab"> 
 
    <button class="tablinks" onclick="openCity(event, '1')" id="defaultOpen2">1</button> 
 
    <button class="tablinks" onclick="openCity(event, '2')">2</button> 
 
    <button class="tablinks" onclick="openCity(event, '3')">3</button> 
 
</div> 
 

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

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

 
<div id="3" class="tabcontent"> 
 
    <span onclick="this.parentElement.style.display='none'" class="topright">x</span> 
 
    <h3>Tokyo</h3> 
 
    <p>Tokyo is the capital of Japan.</p> 
 
</div>

0

を行うには良い方法があります[ない英語のネイティブスピーカー]が

こんにちは。何が起こっているのかは、ただ1つのように2つのタブを使用していることです。各タブを分離し、必要に応じて機能するようにコードを変更する必要があります。ここで

は変更されている部分でコメントを要旨です: https://gist.github.com/rdlagemann/720614ca5ca29726a9f671c0dd2d2385

はそれが役に立てば幸い!

+0

ラファエルはこれも良い解決策です。ありがとうございました。 – user3085351

関連する問題