Home  >  Article  >  Web Front-end  >  Using pictures as labels, the for attribute does not work in IE _HTML/Xhtml_Web page production

Using pictures as labels, the for attribute does not work in IE _HTML/Xhtml_Web page production

WBOY
WBOYOriginal
2016-05-16 16:41:421475browse

For example:

Copy code
The code is as follows:



The effect we hope to get is : Click "Bank of China", the check box will be selected (or canceled). There is no problem in FireFox, CHROME and other browsers, but IE browser does not like this.

The solution is to use JS:

Copy the code
The code is as follows:

window.onload = function(){
if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1 )
{
var a = document.getElementsByTagName("label");
for(var i=0,j=a.length;iif(a[ i].hasChildNodes && a[i].childNodes.item(0).tagName == "IMG")
{
a[i].childNodes.item(0).forid = a[i]. htmlFor;
a[i].childNodes.item(0).onclick = function(){
var e = document.getElementById(this.forid);
switch(e.type){
case "radio": e.checked|=1;break;
case "checkbox": e.checked=!e.checked;break;
case "text": case "password": case "textarea ": e.focus(); break;
}
}
}
}
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn