2013-12-20 30 views
7

標準HTML 5 form label wants an ID to link the label to the input是否可以使用HTML5表單標籤而不泄漏全局變量?

<form> 
    <label for="male">Male</label> 
    <input type="radio" id="male"/> 
    <label for="female">Female</label> 
    <input type="radio" id="female"/> 
</form> 

由於大多數JS開發人員都知道,using IDs leaks globals - 在這種情況下,創建window.malewindow.female

如何在不創建全局變量的情況下使用表單標籤?

回答

10

用另一種方式:

<form> 
    <label>Male <input type="radio"></label>  
    <label>Female <input type="radio"></label>  
</form> 
+0

尼斯。它不僅工作,它更少的代碼! – mikemaccana

相關問題