[英]Is it possible to embed href into a javascript function to open toggle?
I'm using something that requires I use the following html code to open/close a toggle box: 我使用的东西要求我使用以下html代码打开/关闭切换框:
<a href="#myToggle4" class="myToggle4">Open TogglePlus4 on this page</a>
I would like to know if I can trigger from a JavaScript function instead, or maybe how to embed the above code into a JavaScript function. 我想知道是否可以从JavaScript函数触发,或者如何将以上代码嵌入JavaScript函数。
Something like: 就像是:
function openstuff() {
open myToggle4;
}
Yeah, add an event listener for click event. 是的,为click事件添加一个事件侦听器。 Like this. 像这样。
var toggle4a = document.getElementById("t4a");
var toggle4div = document.getElementById("toggle4");
toggle4a.addEventListener("click", function(evt) {
console.log(evt);
console.log(toggle4div.hidden);
if(toggle4div.hidden)
toggle4div.hidden = false;
else
toggle4div.hidden = true;
});
JSFiddle for the same in action - http://jsfiddle.net/u3ac8fx3/ JSFiddle同样适用-http: //jsfiddle.net/u3ac8fx3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.