window.onload = function(){
var wow = document.getElementById("wow");
wow.onclick = function(){
alert("hi");
}
}Run Code Online (Sandbox Code Playgroud)
<label id="wow"><input type="checkbox" name="checkbox" value="value">Text</label>Run Code Online (Sandbox Code Playgroud)
这是我的代码,当我点击"文字"时它会提示两次,但当我点击框时,该onclick元素只会触发一次,为什么?
说我有一套收音机<input>.我不是一个穴居人,所以我知道我需要<label>与那些人<input>交往.由于这里列举的原因,我喜欢将单选按钮包装在相应的标签内.
所以,例如:
<fieldset>
<legend>Should I provide a "for" attribute?</legend>
<label><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_yes" value="yes" />Yep, if you know what's good for you</label>
<label><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_no" value="no" />Nah, that would be redundant and repetitive</label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
此包装将相应的单选按钮与标签相关联.我还需要定义标签的for属性吗?
<fieldset>
<legend>Should I provide a "for" attribute?</legend>
<label for="define_the_for_attribute_yes"><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_yes" value="yes" />Yep, if you know what's good for you</label>
<label for="define_the_for_attribute_no"><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_no" value="no" />Nah, that would …Run Code Online (Sandbox Code Playgroud)