zoukankan      html  css  js  c++  java
  • javascript Uncaught TypeError: Cannot read property ... of undefined

    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">
            &times;
        </a>
        `+str+`
        </div>
        `);
        alertClose(name);
    }

    上面因为调用alertBox在 

      alertBox = $(".alert-box");
    之前,所以alertBox还没定义,是undefined,因而alertBox没有prepend函数

    但初学时很容易看到这个报错就混乱,这里只需要将alertBox = $(".alert-box");放到最前面即可

  • 相关阅读:
    Linux系统下的安装jdk和tomcat教程
    CentOS环境下安装jdk和tomcat
    Java的一个高性能快速深拷贝方法。Cloneable?
    AOP面向切面
    struts中实现ajax的配置信息
    上传下载固定配置
    mysql常用命令
    阿里云部署前后台项目
    PMP相关文件梳理
    面试思路总结
  • 原文地址:https://www.cnblogs.com/cdyboke/p/7422582.html
Copyright © 2011-2022 走看看