admin管理员组

文章数量:1356461

I'm testing one line code on IE7 but it's giving me the following error:

Object doesn't support this property or method and it refers to

Here's the problem line in question:

var checked = document.querySelector('[name="answer1"]:checked');

This is the whole code

Question1 : did you like the training?

<input name="q4" value="Yes" type="radio"> Yes
<input name="q4" value="No" type="radio"> No
<br>
<button id="getval">cick here</button>

<script type="text/javascript"> 
document.getElementById('getval').onclick = function() {
    var checked = document.querySelector('[name="q4"]:checked');
    alert(checked ? checked.value : 'Not selected');
}
</script>

Is there anyway to fix that?

I'm testing one line code on IE7 but it's giving me the following error:

Object doesn't support this property or method and it refers to

Here's the problem line in question:

var checked = document.querySelector('[name="answer1"]:checked');

This is the whole code

Question1 : did you like the training?

<input name="q4" value="Yes" type="radio"> Yes
<input name="q4" value="No" type="radio"> No
<br>
<button id="getval">cick here</button>

<script type="text/javascript"> 
document.getElementById('getval').onclick = function() {
    var checked = document.querySelector('[name="q4"]:checked');
    alert(checked ? checked.value : 'Not selected');
}
</script>

Is there anyway to fix that?

Share Improve this question edited Mar 24, 2013 at 17:26 dsgriffin 68.7k17 gold badges140 silver badges138 bronze badges asked Mar 24, 2013 at 17:10 Rayan SpRayan Sp 1,0207 gold badges18 silver badges29 bronze badges 4
  • which version of IE ? all IE ? – Rishabh Commented Mar 24, 2013 at 17:13
  • I have similar problem in IE10... Any ideas? – Kirill Kobelev Commented Nov 8, 2013 at 21:25
  • @KirillKobelev - you probably have a different problem, as the issue here is that querySelector() is only supported in IE8+. – broofa Commented Nov 17, 2013 at 12:55
  • Thanks. I figured this out... Browser was forced to emulate IE7.. – Kirill Kobelev Commented Nov 17, 2013 at 13:04
Add a ment  | 

4 Answers 4

Reset to default 4

querySelector is only available in IE8+. To query by selector in IE7 you'll need something like Sizzle, which is a standalone version of the CSS query-by-selector code that's built into jquery. Or if you prefer something a bit lighter and more seamless you could use a polyfill such as the one described here, like so:

if (!document.querySelectorAll) {
  document.querySelectorAll = function (selectors) {
    var style = document.createElement('style'), elements = [], element;
    document.documentElement.firstChild.appendChild(style);
    document._qsa = [];

    style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
    window.scrollBy(0, 0);
    style.parentNode.removeChild(style);

    while (document._qsa.length) {
      element = document._qsa.shift();
      element.style.removeAttribute('x-qsa');
      elements.push(element);
    }
    document._qsa = null;
    return elements;
  };
}

if (!document.querySelector) {
  document.querySelector = function (selectors) {
    var elements = document.querySelectorAll(selectors);
    return (elements.length) ? elements[0] : null;
  };
}

old IE doesn't have querySelector. You have to use an engine, like sizzle (that's what's in jQuery), zest, etc

In IE 9+ css3 selector like the one you are using are supported ..

http://caniuse./queryselector

You can refer creating-a-queryselector-for-ie-that-runs-at-native-speed and adding-document-queryselectorall-support-to-ie-7

Okay, you can use jquery $('#selector') as that equals queryselector and you can include jQuery for that.

本文标签: javascriptobject doesn39t support this property or method documentquerySelectorStack Overflow