[英]Label tag has select dropdown, does not work in IE11
在IE上測試的HTML代碼:
<input name="radiogroup" id="x" type="radio"> <label for="x"> test <select> <option value="5">5</option> <option selected="selected" value="10">10</option> <option value="15">15</option> </select> </label>
這是一個示例代碼
嘗試在標簽標簽外移動選擇標簽。
<input name="radiogroup" id="x" type="radio"> <label for="x"> test </label> <select> <option value="5">5</option> <option selected="selected" value="10">10</option> <option value="15">15</option> </select>
您可以設置<select>
的onclick
事件以return false
:
<input name="radiogroup" id="x" type="radio"> <label for="x"> test <select onclick="return false"> <option value="5">5</option> <option selected="selected" value="10">10</option> <option value="15">15</option> </select> </label>
這是因為使onclick
事件返回false會使默認瀏覽器行為不發生,而對於Internet Explorer,默認瀏覽器行為是您不想要的行為,即在用戶打開它后立即關閉下拉列表。
請注意,如果您希望對除Internet Explorer之外的其他瀏覽器執行默認瀏覽器行為,則可以使用Javascript來測試瀏覽器是否為Internet Explorer,並且如果瀏覽器是Internet Explorer,則僅返回false(執行此操作的方法來自此回答 ):
function browserIsIE(){ if(!!navigator.userAgent.match(/Trident/g) || !!navigator.userAgent.match(/MSIE/g)){ return true; } else{ return false; } }
<input name="radiogroup" id="x" type="radio"> <label for="x"> test <select onclick="if(browserIsIE()){return false}"> <option value="5">5</option> <option selected="selected" value="10">10</option> <option value="15">15</option> </select> </label>
Javascript代碼可以更加優雅,但為了清晰起見,我沒有這樣做。
請注意,我在Chrome,Firefox和Edge中測試了這兩個代碼,這兩個代碼在這三種瀏覽器中的行為完全相同,因此您可以使用上述任何一種代碼。
您可以在此處閱讀有關在事件中使用return false
更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.