未捕获的类型错误:无法读取 null(索引):110 的属性'style'单击时



我的警报框有问题。我想设计一个十字架,通过单击它来关闭盒子:

 <div id="alert">
 <img src="cross.png" onclick="document.getElementById(alert).style.display = 'none'" width="15px" height="15px" style="float: right; margin-top: 3px; cursor:pointer;"/>
 <p style="margin-top: 20px;">alert text</p>
 </div>

当我单击十字时,会出现错误。我把它粘贴在标题中。

将"alert"放在引号中。这是一个字符串

你应该在getElementById中的"alert"两边加上单引号 - 该方法需要一个字符串。

所以:

<img src="cross.png" onclick="document.getElementById('alert').style.display = 'none'" width="15px" height="15px" style="float: right; margin-top: 3px; cursor:pointer;"/>

您使用单引号,因为您已经用双引号包装了它。

 <div id="alert">
 <img src="cross.png" onclick="document.getElementById('alert').style.display = 'none'" width="15px" height="15px" style="float: right; margin-top: 3px; cursor:pointer;"/>
 <p style="margin-top: 20px;">alert text</p>
 </div>

最新更新