Javascript error (IE): object does not support this property or method

2015 Note: . Everyone should look at old questions and be encouraged by how far they have come. There are so many errors in this code - this is great.

This code works PERFECTLY in Firefox, and in IE the line ends with "negative"); breaks. I have no idea how to fix this! Argghhh!

$(".topline strong").digits().each(function () {
    s = $(this),
    c = "change";
    if ($(s).hasClass(c) & $(s).text().replace(/%/gi, "") > 0) {
        $(s).addClass("positive");
    }
    if ($(s).hasClass(c) & $(s).text().trim().charAt(0) == "-") $(s).addClass("negative");
});
+3
source share
2 answers
  • Use &&instead &(if this does not mean minimization)
  • String.trimnot yet widely applied. Use

    $(s).hasClass(c) && /^\s*-/.test($(s).text())
    

    instead.

I would rather rewrite the code as:

$(".topline strong").digits().each(function() {
  var s = $(this);
  if (s.hasClass("change")) { // no need to $() an already $()-ed object.
    var value = parseFloat(s.text());
    if (value != 0)
      s.addClass(value > 0 ? "positive" : "negative"); 
  }
});
+2
source

.trim() $.trim() :

$(".topline strong").digits().each(function () {
  var s = $(this), c = "change";
  if (s.hasClass(c) && s.text().replace(/%/gi, "") > 0) { s.addClass("positive"); }
  if (s.hasClass(c) && $.trim(s.text()).charAt(0) == "-") s.addClass("negative");
});

s, jQuery , , :) : .trim() , jQuery $.trim() ( $.inArray(), IE .indexOf()).

var, .


, jQuery 1.4.3+ native String.prototype.trim, $.trim().

+2

Source: https://habr.com/ru/post/1759106/


All Articles