the last tab titled contact me does not display any of its content when I click it.
<body>
<ul class="tabs">
<li data-tab-target="#home" class="active tab">Home</li>
<li data-tab-target="#pricing" class="tab">Pricing</li>
<li data-tab-target="#about" class="tab">About</li>
<li data-tab-target="#Contact Me" class="tab">Contact Me</li>
</ul>
<div class="tab-content">
<div id="home" data-tab-content class="active">
<h1>Home</h1>
<p>This is the home</p>
</div>
<div id="pricing" data-tab-content>
<h1>Pricing</h1>
<p>Some information on pricing</p>
</div>
<div id="about" data-tab-content>
<h1>About</h1>
<p>Let me tell you about me</p>
</div>
<div id="Contact Me" data-tab-content>
<h1>Contact</h1>
<p>hello let me tell</p>
</div>
</div>
</body>
</html>
JS: This is where the error(Uncaught TypeError: Cannot read property 'classList' of null) is and I am not sure what I am doing wrong.
const tabs = document.querySelectorAll('[data-tab-target]')
const tabContents = document.querySelectorAll('[data-tab-content]')
tabs.forEach(tab => {
tab.addEventListener('click', () => {
const target = document.querySelector(tab.dataset.tabTarget)
tabContents.forEach(tabContent => {
tabContent.classList.remove('active')
})
tabs.forEach(tab => {
tab.classList.remove('active')
})
tab.classList.add('active')
target.classList.add('active')
})
})
just remove space in id="Contact Me"
<div id="ContactMe" data-tab-content>
<h1>Contact</h1>
<p>hello let me tell</p>
</div>
It is probably because you are trying to access the Element before it is loaded.
try wrapping your code in window.onload
like this:
window.onload = function () {
// paste your whole JS Code here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.