zero_mumu 2020-06-22 09:15 采纳率: 100%
浏览 250
已采纳

js在函数体中定义了c_href为全局变量为什么仍旧报错ReferenceError: c_href is not defined?

图片说明
图片说明

图中文件chapters_href.txt内容为:

/comic/23885/1408578.html--#--/comic/23885/1395147.html--#--/comic/23885/1387036.html--#--/comic/23885/1360258.html--#--

html,JavaScript代码如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>乌拉乌拉</title>
  </head>
  <body>
    <label>选择章节地址文件:</label>
      <input type="file" id="chapters_hreff" />
      <input type="button" id="b4" value="读取全部文件" />

    <script>
    function readAsText(tem,id){//文本读取
    var t=tem;
    console.log(t);
    file = document.getElementById(id).files[0];  
    reader = new FileReader();//将文件以文本形式读入页面
    reader.readAsText(file);  
    reader.onload=function(f){
    jsArr = JSON.stringify(this.result);//对象转字符串
    tem =jsArr.replace(/"/g,'');//删‘”’
    tem = tem.split("--#--");//分割并且字符串转为数组

    if(t=='chapters_href') {
        c_href = tem;
        console.log(c_href);
    }
   }
}


    var wait=false;
        document.getElementById('b4').addEventListener("click",function(){//监听按下id为b4按钮后再执行内容
            var wait=true;
    if(wait){
        readAsText('chapters_href','chapters_hreff');
        console.log(c_href);
    }
    })
    </script>
  </body>
</html>
  • 写回答

2条回答 默认 最新

  • 菜鸟咸鱼一锅端 2020-06-22 11:20
    关注

    if(t=='chapters_href') {
    c_href = tem;
    console.log(c_href);//这里能打印出来吗?条件进去的情况下报错的吗
    }

        附上代码:这样写
    
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="utf-8" />
        <title>the final project</title>
        <link rel="stylesheet" href="css/finalproject.css" />
        <script src="js/finalproject.js"></script>
        <style>
        </style>
    </head>
    
    <body class="bkgimg">
        <lable>选择文章地址文件:</lable><input type="file" id="chapters_hreff">
        <input type="button" id="b4" value="读取全部文件">
    
        <script>
            var c_href = ''
            var wait = false
            function readAsText(tem,id){
                var t = tem;
                console.log('------',t)
                file = document.getElementById(id).files[0];
                reader = new FileReader();
                reader.readAsText(file)
                reader.onload = function(f){
                    jsArr = JSON.stringify(this.result);
                    tem = jsArr.replace(/"/g,'');
                    tem = tem,split("--#--");
                    if(t=='chapters_href'){
                        c_href = tem;
                        console.log('进来了吗?',c_href)
                    }
                }
            }
            document.getElementById('b4').addEventListener('click',function(){
                wait = true
                if(wait){
                readAsText('chapters_href','chapters_hreff');
                console.log('这里呢',c_href)
                }
            })
    
        </script>
    </body>
    
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?