2017-10-25 6 views
0

こんにちは、Javascriptを使用してカード内の段落を表示および非表示にするコードを書きました。これは最初のカードで完璧に動作しますが、他のカードでは動作しません。これはあまり難しいことではありませんが、学校のプロジェクトであり、いくつかのルールがあります。 div、class、idは使用できません。意味論的なものでなければならないので、チェックボックスのハックとonclick属性は許可されません。JavascriptはクラスまたはIDを使用せずに複数のボタンで切り替える

これは私のコードです:

var section = document.querySelectorAll('section > summary > p'); 
 
var button = document.querySelectorAll('section > summary > button'); 
 

 
var show = function() { 
 
\t section.classList.toggle('show') 
 
} 
 

 
button.addEventListener('click', show());
section summary p { 
 
    display: none; 
 
} 
 

 
section summary p.show { 
 
    display: block; 
 
}
<body> 
 
    <main> 
 

 
    <!--first card--> 
 

 
    <section> 
 
     <!-- Top part--> 
 
     <span> <img> </span> 
 

 
     <!-- Bottom part--> 
 
     <summary> 
 

 
     <button>show paragraph</button> 
 
     <!--This button triggers the toggle--> 
 

 
     <h2></h2> 
 
     <h3></h3> 
 

 
     <p>I'm trying to show and hide this p <a href="#">lees meer</a></p> 
 

 
     <footer></footer> 
 

 
     </summary> 
 

 
    </section> 
 

 
    <!--second card--> 
 

 
    <section> 
 
     <!-- Top part--> 
 
     <span> <img> </span> 
 

 
     <!-- Bottom part--> 
 
     <summary> 
 

 
     <button>show paragraph</button> 
 

 
     <h2></h2> 
 
     <h3></h3> 
 

 
     <p>I'm trying to show and hide this p <a href="#">lees meer</a></p> 
 

 
     <footer></footer> 
 

 
     </summary> 
 

 
    </section> 
 
    </main> 
 
</body>

これは私のペンへのリンクです: https://codepen.io/SummerD/pen/MEMMNB

私はクラスを使用せずに、この問題への解決策を見ていません。あなたが私を助けることを願っています!この割り当ての性質、およびクリーンHTMLとCSSを使用するクラスの欠如を考えると

+0

'querySelector'は1つの項目を返します。 'querySelectorAll'を使用してください。 – evolutionxbox

+0

私はちょうどこれを試してみたところ、両方とも動作しません@evolutionxbox – Summer

+0

どこで使ったのですか?あなたはそれを使用する方法を理解していますか? – evolutionxbox

答えて

1

は、私はJavaScriptを伸ばして少し船外に行ってきました:

// Collect all buttons, and store them in an Array using querySelectorAll 
var buttons = document.querySelectorAll('button') 

// Initiate a for loop that 'loops' through all options in the buttons array. 
for (var i = 0; i < buttons.length; i++) { 
    // For every instance of buttons in the Array, apply an event listener (click). 
    buttons[i].addEventListener('click', function(e) { 
    // Grab the target of the click event, select their parent and access the 7th child (the <p> tag in question). Access the classList and toggle the 'show' class. 
    e.target.parentNode.childNodes[7].classList.toggle('show'); 
    }); 
}; 

通常は、このタイプをしないだろうそれは読みにくく、常に変化する一貫性のあるDOMに依存しているため、コード化されていません。それはあなたの例のために適切に動作します。

0

これはこれを行う1つの方法です。私はすべてのカードを選択することで、イベントリスナーを追加する方法

var cards = document.querySelectorAll('section'); 
 

 
cards.forEach((card)=>{ 
 
    card.querySelector('button').addEventListener('click', function(){ 
 
    card.querySelector('p').classList.toggle('show'); 
 
    }) 
 
})
* { 
 
\t box-sizing: border-box; 
 
\t font-family: Segoe UI, Myriad, Verdana, sans-serif; 
 
} 
 
body { 
 
\t background-color: grey; 
 
    
 
} 
 
main { 
 
\t display: flex; 
 
\t flex-flow: row wrap; 
 
\t justify-content: center; 
 
} 
 

 
/*================= 
 
    =====the card==== 
 
    =================*/ 
 
    
 
section { 
 
\t width: 15em; 
 
\t margin: 2em; 
 
\t box-shadow: 0 0.2em 0.4em 0 rgba(0, 0, 0, 0.20); 
 
\t background-color: white; 
 
\t max-height: 23em; 
 
} 
 

 

 
/*top part*/ 
 

 
span { 
 
\t display: block; 
 
\t overflow: hidden; 
 
\t position: relative; 
 
} 
 

 
span img { 
 
\t width: 120%; 
 
} 
 
/*bottom part*/ 
 
section summary { 
 
\t bottom: 0; 
 
\t background: white; 
 
\t width: 100%; 
 
\t padding: 1em; 
 
} 
 
section summary h2 { 
 
\t margin: 0; 
 
\t padding-bottom: 0.5em; 
 
\t color: black; 
 
\t font-size: 1.5em; 
 
\t font-weight: 700; 
 
} 
 
section summary h3 { 
 
\t margin: 0; 
 
\t padding: 0 0 1em; 
 
\t color: darkred; 
 
\t font-size: 1em; 
 
\t font-weight: 400; 
 
} 
 
    /*i'm trying to show/hide this p on click of the button*/ 
 
section summary p { 
 
\t color: black; 
 
\t font-size: 0.8em; 
 
\t line-height: 1.8em; 
 
    display:none; 
 
} 
 
section summary p.show { 
 
    display:block; 
 
} 
 
section summary footer { 
 
\t margin: 2em 0 0; 
 
\t color: black; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>Cards</title> 
 
</head> 
 
<body> 
 
<body> 
 
<main> 
 
<!--first card--> 
 
    <section> 
 
    <!-- Top part--> 
 
    <span> 
 
\t \t \t <img src="https://www.w3schools.com/css/img_fjords.jpg"/> </span> 
 
    <!-- Bottom part--> 
 
    <summary> 
 
     <button>show paragraph</button> 
 
     <h2>Moe</h2> 
 
     <h3>leeg</h3> 
 
     <p>De buschauffeur port me wakker. We zijn bij de eindhalte, Centraal Station. Ik stap uit en duw de kinderwagen voort waarin mijn zoon ligt te slapen. Ik wou dat ik hem was. De mensen lopen in zichzelf gekeerd over het plein. De regen doet z’n best 
 
     mij wakker te houden, maar mijn oogleden voelen zwaar, alsof er een stel verveelde kaboutertjes aan lopen te trekken. <a href="#">lees meer</a> </p> 
 
     <footer>6 Minuten</footer> 
 
    </summary> 
 
    
 
    </section> 
 
    <!--first card--> 
 
    <section> 
 
    <!-- Top part--> 
 
    <span> 
 
\t \t \t <img src="https://www.w3schools.com/css/img_fjords.jpg"/> </span> 
 
    <!-- Bottom part--> 
 
    <summary> 
 
     <button>show paragraph</button> 
 
     <h2>Moe</h2> 
 
     <h3>leeg</h3> 
 
     <p>De buschauffeur port me wakker. We zijn bij de eindhalte, Centraal Station. Ik stap uit en duw de kinderwagen voort waarin mijn zoon ligt te slapen. Ik wou dat ik hem was. De mensen lopen in zichzelf gekeerd over het plein. De regen doet z’n best 
 
     mij wakker te houden, maar mijn oogleden voelen zwaar, alsof er een stel verveelde kaboutertjes aan lopen te trekken. <a href="#">lees meer</a> </p> 
 
     <footer>6 Minuten</footer> 
 
    </summary> 
 
    
 
    </section> 
 
</main> 
 
</body> 
 
</body> 
 
</html>

お知らせ。

関連する問題