简体   繁体   中英

getElementsByClassName doesn't work on IE6. What am I doing wrong?

My script works on IE7, 8, 9, etc., Chrome, Firefox, Safari, etc. But when I run this on IE6, it doesn't work. It is a calculate function like a bet vs cash on bank = transfer.

What am I doing wrong?

Here's a working sample :

 var a, p = document.getElementsByClassName("preAmount"); var mark = new Array(); $(function () { $('input').each(function () { $(this).keyup(function () { var w = $(this).val(), n = w.indexOf("."), z = w.indexOf("0"); //allow 2 digit decimal if (n > 0 && (w.length - n) > 3) { $(this).val(parseFloat(w.substring(0, n) + w.substring(n, n + 3))); } //reset beginning with zero if (z == 0) { $(this).val(w * 1); } //set zero if (isNaN(w) || w.length == 0) { $(this).val("0"); } calculateTotal($(this)); }); }); $(".enableOnInput").click(function () { var submitValid = false; if (mark.length > 0) { for (k = 0; k < mark.length; k++) { // if one of the mark in array is true, it's valid to submit if (mark[k] == true) { submitValid = true; break; }; } } if (submitValid) { window.open("success.html", "_self"); } else { alert('您尚未更新游戏平台转帐资金!\\n\\nYou have not alter transfer amount!'); } }); $(".reset").click(function () { a = document.getElementsByClassName('namount'); for (k = 0; k < a.length; k++) { a[k].value = parseFloat(p[k].innerHTML, 10); } $('.balance-value').text($('.t-right-title-balance').text()); mark = new Array(); }); }); function cal() { var ta = 0, tp = 0, tb = parseFloat($('.t-right-title-balance').text()); a = document.getElementsByClassName('namount'); for (j = 0; j < p.length; j++) { var ttp, tta; ttp = parseFloat(p[j].innerHTML); tta = parseFloat(a[j].value); tp += ttp; ta += tta; mark[j] = (ttp != tta); //set alter mark of each platform } return (tb + tp - ta); } function calculateTotal(src) { var sumtable = src.closest('.sumtable'); sumtable.find('input').each(function () { var preAmount = $(this).parent().parent().find('.preAmount').text(); var curAmount = this.value; if (!isNaN(this.value) && this.value.length != 0) { preAmount = parseFloat(preAmount); curAmount = parseFloat(this.value); var f = parseFloat($('.t-right-title-balance').text()).toFixed(2); var transAmount = curAmount - preAmount; if (curAmount < 0 || transAmount > f) { this.value = preAmount; $('.balance-value').text(f); return; } else { var b = parseFloat($('.balance-value').text()); var tb = cal(); if (tb >= 0) { $('.balance-value').text(tb.toFixed(2)); } else { this.value = preAmount; //illegal rollback $('.balance-value').text(tb.toFixed(2)); return; } } } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

I don't know to tag the correct answer, but this is what fixed it,

From: @Phil - I imagine you'd have a little trouble with document.getElementsByClassName (minimum IE support is v9). Use the jQuery selector instead, eg $('.namount')

According to W3C: The getElementsByClassName() method is not supported in Internet Explorer 8 and earlier versions.

I'm pretty sure that's right.

<!DOCTYPE html>
<html>
<body>
<p class="demo">Test.</p>
<button onclick="myFunction()">Click me!</button>
<script>
      function myFunction() {
          document.getElementsByClassName('demo').innerHTMl = "Hallo!";
      }
</script>
</body>
</html>

You can try this on Internet Explorer 6 or 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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