[英]Manipulate element onclick base on data-target
So I'm trying to make a button which toggles the visibility of a div element.所以我正在尝试制作一个按钮来切换 div 元素的可见性。 But the approach would be getting the value of data-target
of the button and manipulate the correspoding div element that has an id
equal to the data-target value.但是该方法将获取按钮的data-target
的值并操作具有等于数据目标值的id
的相应 div 元素。 I'm doing this so I could reuse the javascript function to different buttons that has their own corresponding div element to toggle visibility.我这样做是为了可以将 javascript 函数重用于具有自己相应 div 元素的不同按钮来切换可见性。
so far here is my html code到目前为止,这是我的 html 代码
<header id="header">
<div class="header">
<div class="nav-container">
<a href="{{ route('home') }}" class="nav-brand">
<img src="{{ asset('logo_brandmark.png') }}" alt="logo_brandmark.png" class="nav-brandmark"><span class="nav-wordmark">{{ config('app.name', 'app name') }}</span>
</a>
<button class="btn btn-primary" data-target="navAuth" onclick="btnToggleVisibility(this)"><i class="fa fa-bars" aria-hidden="true"></i></button>
</div>
<div class="collapse" id="navAuth">
<a href="{{ route('login') }}" class="auth-link btn btn-primary">{{ __('Login') }}</a>
<a href="{{ route('register') }}" class="auth-link btn btn-info">{{ __('Register') }}</a>
</div>
</div>
</header>
and this is the external javascript这是外部的javascript
function btnToggleVisibility(obj) {
var targetElement = obj.getAttribute('data-target');
targetElement.classList.toggle('show');
}
window.btnToggleVisibility = btnToggleVisibility;
when I click on the button, it shows this error当我单击按钮时,它显示此错误
you forgot the element that corresponds to the id value您忘记了与 id 值对应的元素
function btnToggleVisibility(obj) {
var targetElement = document.getElementById(obj.getAttribute('data-target'));
targetElement.classList.toggle('show');
}
window.btnToggleVisibility = btnToggleVisibility;
You can also read data-*
attribute values using the dataset
property that is available on the element.您还可以使用元素上可用的dataset
属性读取data-*
属性值。
You could also restructure how you're going about this so that you're not defining or attaching the toggle
directly on elements using the global onclick
event handler.您还可以重组您的处理方式,这样您就不会使用全局onclick
事件处理程序直接在元素上定义或附加toggle
。 Might make future changes simpler for you if things are in a centralised location.如果事情在一个集中的位置,可能会使您将来的更改更简单。
For example, you could achieve your outcome as follows:例如,您可以实现如下结果:
// the CSS class we'll toggle
.hidden {
visibility: hidden;
}
// the html
<button type="button" class="toggler" data-target="#div-1">Show/Hide</button>
<div class="hidden" id="div-1">
<p>This is the content that will be toggled</p>
</div>
document.addEventListener('DOMContentLoaded', (e) => {
// define the function that will toggle the targets visibility using the .hidden class
function toggleTargetVisibility(e) {
// get all elements that match the target data-* attribute value
// there may or may not be more than one element
let div = document.querySelectorAll(e.dataset.target);
// toggle the .hidden class for each found element
div.forEach(function(el) {
el.classList.toggle('hidden');
})
}
// get all elements that will be responsible for triggering the toggle function
let togglers = document.querySelectorAll('.toggler');
// attach the onclick event to each found toggler element
togglers.forEach(function(toggler) {
toggler.addEventListener('click', function(e) {
toggleTargetVisibility(e.target);
});
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.