Uncaught TypeError:无法设置null的属性'href' [英] Uncaught TypeError: Cannot set property 'href' of null

查看:282
本文介绍了Uncaught TypeError:无法设置null的属性'href'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我试图从网格中下载文件。


    • 当我点击打开第一个窗口链接时,一个弹出窗口打开。

    • 在弹出窗口中,您将看到一个网格。 / li>
    • 但是现在你会看到网格,但是在下面的行发生错误。
    • 我试图下载这个文件,就像这个功能
      https://jsfiddle.net/335t5aow/

    • ul>


    未捕获的TypeError:无法设置null的属性'href'



    template:< a class = \sportsDataPlayer \onclick = \窗口.open('#= FileName#','popup','width = 800,height = 600,scrollbars = yes,resizable = no')\>#= FileName.substring(FileName.lastIndexOf('/' )+1)#< / a>




    • 你们可以告诉我如何解决它。

    • 提供以下代码



    http://jsfiddle.net/savyjgg2/2/

      template:< a class = \sportsDataPlayer\onclick = \window.open('#= FileName#','popup ','width = 800,height = 600,scrollbars = yes,resizable = no')\>#= FileName.substring(FileName.lastIndexOf('/')+ 1)#< / a> 
    var csv =a,b,c\\\
    1,2,3\\\
    ;
    var data = new Blob([csv]);
    var a2 = document.getElementById(。sportsDataPlayer);
    alert(我在这里);
    a2.href = URL.createObjectURL(data);


    解决方案

    更新 p>

    Ja Ja 我已更新小提琴,但它是只有部分锻炼(只有第一个文件将被下载)。要将文件呈现为可下载格式,您必须使用 <$ < a> 标记中的c $ c> download 属性。

    在你的JS代码中,你必须相应地改变逻辑,这样 下载 属性将分别应用于每个文件名。



    试试这 FIDDLE ,只是一个小小的更正



    变化是,


    1. var a2 = document.getElementById(sportsDataPlayer );

    2. class = \sportsDataPlayer\更改为 id = \sportsDataPlayer \


    • I am trying to download the file from the grid.
      • when I click open first window link a popup opens.
      • in that popup you will see a grid.
      • but right now you will see the grid but an error is happening at the below line.
      • I am trying to download the file like this functionality https://jsfiddle.net/335t5aow/

    Uncaught TypeError: Cannot set property 'href' of null

    template: "<a class=\"sportsDataPlayer\" onclick=\"window.open('#= FileName#', 'popup', 'width=800,height=600,scrollbars=yes,resizable=no')\">#= FileName.substring(FileName.lastIndexOf('/')+1) #</a>"
    

    • can you guys tell me how to fix it.
    • providing code below

    http://jsfiddle.net/savyjgg2/2/

    template: "<a class=\"sportsDataPlayer\" onclick=\"window.open('#= FileName#', 'popup', 'width=800,height=600,scrollbars=yes,resizable=no')\">#= FileName.substring(FileName.lastIndexOf('/')+1) #</a>"    
    var csv = "a,b,c\n1,2,3\n";
    var data = new Blob([csv]);
    var a2 = document.getElementById(".sportsDataPlayer");
    alert("I am here");
    a2.href = URL.createObjectURL(data);
    

    解决方案

    Update:

    Ja Ja I have updated the fiddle, but it is only a partial workout (only first file will be downloaded). To render the file as downloadable you'll have to use download attribute in <a> tag.

    And in your JS code, you'll have to make changes in logic accordingly so that download attribute will be applied to each file name separately.

    Try this FIDDLE, there was just a small correction

    Changes were,

    1. var a2 = document.getElementById("sportsDataPlayer");
    2. Changed from class=\"sportsDataPlayer\" to id=\"sportsDataPlayer\"

    这篇关于Uncaught TypeError:无法设置null的属性'href'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆