【问题标题】:Uncaught TypeError: Cannot set property 'src' of null for ImageButton未捕获的类型错误:无法为 ImageButton 设置属性“src”为 null
【发布时间】:2016-07-09 10:53:09
【问题描述】:

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

我想在特定条件下 (t>80) 更改 ImageButton 的 ImageURL,但我得到的是 未捕获的 TypeError:无法设置属性 'src' of 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;"/> 

为什么会出现这个错误?

【问题讨论】:

  • 如果 ImageButton 放置在内容页面中,则 DOM 找不到 id

标签: javascript jquery imagebutton imageurl


【解决方案1】:

ID 将与您在服务器端代码中设置的不同。将btnV11 替换为&lt;%= btnV11.ClientID %&gt;

$(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);
    });

【讨论】:

  • 按照您的建议工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 2014-06-26
  • 2018-01-03
  • 2014-02-07
  • 2014-03-06
相关资源
最近更新 更多