[英]Trigger add with pure JavaScript
I want to add a trigger on the navbar toggle button when click a link with pure javascript. 单击纯JavaScript链接时,我想在导航栏切换按钮上添加触发器。 Not Jquery.
不是Jquery。 I tried with this code but it's not working.
我尝试使用此代码,但无法正常工作。
closeNavbar() {
document.getElementById("collaspe-btn").addEventListener("click");
}
<Navbar.Toggle
id="collaspe-btn"
/>
<Link
to="features"
onClick={this.closeNavbar}
>
To trigger click event you can use .click()
method. 要触发单击事件,可以使用
.click()
方法。 like, 喜欢,
closeNavbar() {
document.getElementById("collaspe-btn").click();
}
addEventListener accepts three parameters the first being the event, the other being a function that specifies the function to run when the event occurs and the third (optional) being a boolean value, which specifies whether the event should be executed in the capturing or in the bubbling phase. addEventListener接受三个参数,第一个为事件,另一个为指定事件发生时要运行的函数的函数,第三个(可选)为布尔值,该布尔值指定是在捕获中还是在事件中执行事件。起泡阶段。
document.getElementById("collaspe-btn").addEventListener("click", function(){
//do something
});
Or, 要么,
document.getElementById("collaspe-btn").addEventListener("click", clickHandler);
function clickHandler(){
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.