報錯:jquery3.1.1報錯Uncaught TypeError: a.indexOf is not a function

jquery3.1.1,怎麼用的時候報錯Uncaught TypeError: a.indexOf is not a function,換成1.9就能夠了!
使用的時候是用一個刮獎的插件,代碼以下:jquery

<script src="jquery-3.1.1.min.js"></script>
<script src="wScratchPad.min.js"></script>
<script> $("#mask_index").wScratchPad({ size : 40, bg : "", fg : "p1_bg.jpg", realtime : false, scratchDown : null, scratchUp : function(e, percent){ if(percent > 2){ this.clear(); this.enable("enabled", false); $("#mask_index").hide(300); } }, scratchMove : function(e, percent){ console.log(percent); }, cursor: "crosshair" }); </script>

只要換回1.9就沒問題了,插件也不須要用3.1,只是好奇新版本有什麼不同的?app

我把你的問題復現了一下,而後開始debug,先說下思路,我能看到jq裏哪裏出錯了,但不知道是哪裏跳到那裏的ssh

  1. 參照你說的這個庫的demo一個個的排除掉可能的參數引發的錯誤,發現是fg參數引發的錯誤,若是是色值就不會報錯,說明是圖片的處理代碼出了問題ide

  2. 找處處理fg的代碼,格式化代碼,是這裏的問題this

a(new Image) .attr("src", this.options.fg) .load(function () { b.ctx.drawImage(this, 0, 0, c, d), b.$img.show() })

3.debugger一下,果真是從這裏到了報錯的地方spa

4.對比jq高低版本的這段代碼插件

    3.1.1是這樣的debug

r.fn.load = function (a, b, c) { var d, e, f, g = this, h = a.indexOf(" ");

1.9.0是這樣的code

st.fn.load = function (e, n, r) { if ("string" != typeof e && Rn) return Rn.apply(this, arguments); var i, o, a, s = this, u = e.indexOf(" ");

報錯也正是你說的位置,能夠看到1.9.0比3.1.1多了一個處理,相信是通過這個處理,e.indexOf這個方法沒有報錯blog

5.兩段代碼運行到這裏的變量都是這樣,

這是1.9.0通過處理的變量狀況

這是3.1.1沒通過處理的變量狀況

 

 

ps:

碰巧遇到高版本jquery報此錯,百度一番,看到一位外國網友很好的回答。具體以下:

This error might be caused by jquery event aliases like .load, .unload or .error deprecated since jQuery 1.8. Look for these aliases in your code and replace them with .on() to register listeners instead. Example:

$(window).load(function(){...});

becomes:

$(window).on('load', function(){ ...});

大概意思:$(window).load(function(){})在高版本中已經廢棄,

相關文章
相關標籤/搜索