最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Radio Button Style not working in Internet Explorer 8 - Stack Overflow

programmeradmin7浏览0评论

I am having few radio buttons. I use the following css for changing the radio button style with an image. It works perfectly in firefox, chrome and even Internet Explorer 9 but not in Internet Explorer 8.

input[type='radio'] + label {
margin: 0;
clear: none;
padding: 5px 0 4px 24px;
/* Make look clickable because they are */
cursor: pointer;
background: url(icons.png) left center no-repeat;
background-position:0px -7055px; width:45px; height:20px; border:0px; cursor:pointer
}

input[type='radio']:checked + label {
background-image: url(icons.png);
background-position:-54px -7055px; width:45px; height:20px; border:0px; cursor:pointer
}

I am having few radio buttons. I use the following css for changing the radio button style with an image. It works perfectly in firefox, chrome and even Internet Explorer 9 but not in Internet Explorer 8.

input[type='radio'] + label {
margin: 0;
clear: none;
padding: 5px 0 4px 24px;
/* Make look clickable because they are */
cursor: pointer;
background: url(icons.png) left center no-repeat;
background-position:0px -7055px; width:45px; height:20px; border:0px; cursor:pointer
}

input[type='radio']:checked + label {
background-image: url(icons.png);
background-position:-54px -7055px; width:45px; height:20px; border:0px; cursor:pointer
}
Share Improve this question asked Dec 25, 2012 at 3:03 Santron ManibharathiSantron Manibharathi 6425 gold badges12 silver badges27 bronze badges 1
  • jsfiddle/b2ERd - here it is available in jsfiddle – Santron Manibharathi Commented Dec 25, 2012 at 3:13
Add a ment  | 

3 Answers 3

Reset to default 5

the :checked pseudo class is not available in IE8

http://www.quirksmode/css/contents.html

http://www.quirksmode/css/enabled.html

I just show normal radio buttons in IE8 and lower. This gives people with old technology the same experience and doesn't burden you with hours/days of work just to have everything look the same across browsers.

Only for IE 8 and above. you can use

input[type='radio'][checked] + label {
background-image: url(icons.png);
background-position:-54px -7055px; width:45px; height:20px; border:0px; cursor:pointer
}

This will work in IE8 and above but not in IE7 and below..

I tried all polyfills out there. Except ie9.js none of them worked. Unfortunately ie9.js made my page load as slow as a herd of snails traveling through peanut butter. After nearly 2(!) plete days of cursing I finally got that piece of IE8 radio button working with a little bit of jQuery and CSS.

First problem was to add and remove a class to the un-/checked input. Second was to force IE8 to rerender the unchecked input once another radio button was checked instead. I think this approach will work with checkboxes, too.

jQuery:

// maybe you need this next small line, I didn't
// $('input:checked').addClass("selected");
$('input').change(function () {  // click() worked too but change is safer
  $(this).addClass("selected");
  $('input:not(:checked)').removeClass("selected");
  // adding a class to a wrapping element
  // and then remove it to force IE8 to rerender
  var testClass = "testClass";       
  $(this).parent().parent().addClass(testClass).removeClass(testClass);
});

And the CSS:

input[type="radio"] {
  // display: none; won't work
  // so replace the actual button offscreen
  // or cover it with following label
}
input[type="radio"] + label {
  background-color: red;  // or background-image
}
input[type="radio"].selected + label {
  background-color: green;  // or background-image
}
input[type="radio"] + label,
input[type="radio"].selected + label {
  position: relative;
  top: 150px;
  left: 300px;
  height: 48px;
  width: 48px;
  display:inline-block;
  padding: 0;
  text-indent: -9999px;
  cursor: pointer;
}
发布评论

评论列表(0)

  1. 暂无评论