简体   繁体   English

尝试通过onclick调用函数时未定义值

[英]Value is not defined when trying to call with function with onclick

When im trying to send(onclick) value to toggle_sprint() function i get error that value is not defined. 当我试图发送(onclick)值到toggle_sprint()函数时,我得到错误,该值未定义。 Why is it not defined? 为什么未定义? I thought it got defined when i put value="false" but apparently not. 我以为当我输入value =“ false”时就定义了它,但显然没有。

<div class="select">
    <div id="toggle_sprint" value="false" onclick="toggle_sprint(value);" >Toggle Sprint</div>

</div>

There is no value attribute for div elements… and div elements aren't designed to be interactive so you're creating a bunch of accessibility issues by slapping onclick on a div. div元素没有value属性……而且div元素并非设计为交互式的,因此您通过在div上单击onclick来创建一系列可访问性问题。

Additionally, your use of an intrinsic event attribute means that toggle_sprint() probably isn't what you think it is . 此外,您使用固有事件属性意味着toggle_sprint()可能与您认为的不一样

Start by writing good HTML. 首先编写好的HTML。

Use data-* attributes to provide data just for your JS. 使用data-*属性仅为您的JS提供数据。 Use buttons to provide things to click on and trigger JS. 使用按钮提供单击和触发JS的功能。

Then bind your event handlers with JavaScript. 然后将事件处理程序与JavaScript绑定。

  document.getElementById("toggle_sprint").addEventListener("click", function(event) { toggle_sprint(this.dataset.value); }); function toggle_sprint(arg) { console.log(arg); } 
 <div class="select"> <button type="button" id="toggle_sprint" data-value="false">Toggle Sprint</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM