未捕获的类型错误:无法为 ImageButton 设置属性 'src' 的 null

Uncaught TypeError: Cannot set property 'src' of null for ImageButton

本文关键字:设置 属性 null src ImageButton 类型 错误      更新时间:2023-09-26

我已经搜索了类似的问题,但我没有找到适合我的东西。

我想在特定条件 (t>80) 下更改图像按钮的图像URL,但我得到的是未捕获的类型错误:无法将属性"src"设置为 null。我的<head>代码是:

    $(document).ready(function () {
           //doing other stuffs
        setInterval(function () {
                         $(".industrial.tank.size.one").each(function () {
                            $(this).industrial(t);
                            if (t > 80) {
                                // document.getElementById('btnV11').ImageURL = "~/Images/Valve/Valve-gray-left.png";
                                document.getElementById('btnV11').src = "../../Images/Valve/Valve-gray-left.png";           
                        });  
        }, 3000);
    });

<body>

<asp:ImageButton ID="btnV11" runat="server" Height="51px" ImageUrl="~/Images/Valve/Valve-alarm-left.gif" Width="53px" OnClientClick="ShowControlPanel_P11(); return false;"/> 

为什么我会收到此错误?

id 将

不同于您在服务器端代码中设置的 id。 将btnV11替换为<%= btnV11.ClientID %>

$(document).ready(function () {
           //doing other stuffs
        setInterval(function () {
                         $(".industrial.tank.size.one").each(function () {
                            $(this).industrial(t);
                            if (t > 80) {
                                // document.getElementById('btnV11').ImageURL = "~/Images/Valve/Valve-gray-left.png";
                                document.getElementById('<%= btnV11.ClientID %>').src = "../../Images/Valve/Valve-gray-left.png";           
                        });  
        }, 3000);
    });