如果源为空,如何将带有 id 的 img 标签设置为 false?

How to set an img tag with an id to false if the source is empty?

如果内部 html 没有分配图像源,我正在尝试将 ID 为 #myComputer 的 img 标签的可见性设置为 false。

HTML

    <img id="myComputer" src="assets/myComputer.png">

JS

    var myComputer = document.getElementById('myComputer').src;
    var myComputerVisible = true;

    if(myComputer == ""){
    myComputerVisible = false;
    };

当我删除源并检查控制台时 'myComputerVisible' 仍然显示为 "true" 即使我删除了 html 中的源:

    <img id="myComputer" src="">

仅普通 Javascript。请指教!谢谢!

请使用 getAttribute 获取 HTML 标签的任何属性值。

 var myComputer = document.getElementById("myComputer").getAttribute("src");

改用getAttribute

var myComputer = document.getElementById('myComputer').getAttribute("src");
var myComputerVisible = true;
console.log("src is: " + myComputer);
if (myComputer == "") {
  myComputerVisible = false;
};

console.log(myComputerVisible);
<img id="myComputer" src="">