简体   繁体   English

标签标签有选择下拉列表,在IE11中不起作用

[英]Label tag has select dropdown, does not work in IE11

html code to test on IE : 在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> 

This is a sample code 这是一个示例代码

Try moving the select tag outside the label tag. 尝试在标签标签外移动选择标签。

 <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> 

You can set the onclick event of the <select> to return false : 您可以设置<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> 

This works because making the onclick event return false keeps the default browser behavior from taking place, and for Internet Explorer, the default browser behavior is the behavior that you don't want, that is closing the dropdown right after the user opened it. 这是因为使onclick事件返回false会使默认浏览器行为不发生,而对于Internet Explorer,默认浏览器行为是您不想要的行为,即在用户打开它后立即关闭下拉列表。

Note that if you want the default browser behavior to take place for other browsers than Internet Explorer, you can use Javascript to test if the browser is Internet Explorer and only return false if the browser is Internet Explorer (the method for doing this comes from this answer ): 请注意,如果您希望对除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> 

The Javascript code could be made a lot more elegant, but I didn't do so for the sake of clarity. Javascript代码可以更加优雅,但为了清晰起见,我没有这样做。

Note that I've tested both codes in Chrome, Firefox and Edge and both codes had exactly the same behavior in these three browsers, so you can use whichever of the above codes you want. 请注意,我在Chrome,Firefox和Edge中测试了这两个代码,这两个代码在这三种浏览器中的行为完全相同,因此您可以使用上述任何一种代码。

You can read more about using return false with events here . 您可以在此处阅读有关在事件中使用return false更多信息。

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

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