javascript Uncaught TypeError: Cannot read property ... of undefined
2017-08-24 13:37
691 查看
main.js:26 Uncaught TypeError: Cannot read property 'prepend' of undefined at alertSuccess (main.js:26) at HTMLDocument.<anonymous> (main.js:11) at j (jquery.js:3073) at Object.fireWith [as resolveWith] (jquery.js:3185) at Function.ready (jquery.js:3391) at HTMLDocument.I (jquery.js:3407)
"use strict"; var pageSize = 10 //全局pageSize , messageBoxNum = 0 , alertBox ; //共用函数 $(document).ready(function() { alertSuccess(screen.availWidth+" "+screen.availHeight) alertBox = $(".alert-box"); alertBox.css("left",$("html").width()/2-alertBox.width()/2); }); function alertClose(name){ function close(){ alertBox.children("div[name='"+name+"']").remove(); } setTimeout(close,3000); } function alertSuccess(str){ var name="messageBox"+messageBoxNum; messageBoxNum++; alertBox.prepend(` <div class="alert alert-success" name="`+name+`"> <a href="#" class="close" data-dismiss="alert"> × </a> `+str+` </div> `); alertClose(name); }
上面因为调用alertBox在
alertBox = $(".alert-box");
之前,所以alertBox还没定义,是undefined,因而alertBox没有prepend函数
但初学时很容易看到这个报错就混乱,这里只需要将alertBox = $(".alert-box");放到最前面即可
相关文章推荐
- JsTree树不出来 报Uncaught TypeError: Cannot read property 'children' of undefined
- Uncaught TypeError: Cannot read property 'fn' of undefined
- Uncaught TypeError: Cannot read property 'number' of undefined
- extjs4:Uncaught TypeError: Cannot read property 'processed' of undefined
- background.js Uncaught TypeError: Cannot read property 'xxx' of undefined
- bug之bootstrap switch Uncaught TypeError: Cannot read property 'apply' of undefined
- 浏览器报Uncaught TypeError: Cannot read property 'trigger' of undefined解决方案
- dataTables Uncaught TypeError: Cannot read property 'style' of undefined
- Uncaught TypeError: Cannot read property 'decimalSeparator' of undefined
- Uncaught TypeError: Cannot read property 'length' of undefined
- Uncaught TypeError: Cannot read property 'msie' of undefined
- util.js:68 Uncaught TypeError: Cannot read property 'fn' of undefined
- tankgame2.js:37 Uncaught TypeError: Cannot read property 'direct' of undefined(…)
- 使用form的方法(例如:reset,clear等)报错Uncaught TypeError: Cannot read property 'tip' of undefined(…)的原因
- jquery.dataTables.min.js:62 Uncaught TypeError: Cannot read property 'style' of undefined原因
- echarts.min.js:25 Uncaught TypeError: Cannot read property 'features' of undefined
- DataTable插件报错:Uncaught TypeError: Cannot read property 'style' of undefined
- Uncaught TypeError: Cannot read property 'msie' of undefined
- Uncaught TypeError: Cannot read property 'substring' of undefined
- 浏览器报Uncaught TypeError: Cannot read property 'trigger' of undefined解决方案