Home  >  Q&A  >  body text

javascript - How to determine whether the value of src is empty?

<p class="section benefit-section cftx">
        <img class="max-img" src="" alt="无图片">
        <img class="max-img" src="" alt="无图片">


</p>
    <script type="text/javascript">
        window.onload = function() {
            var imglength = document.getElementsByClassName("max-img");
            alert(imglength[0].attribute("src"));

            if (imglength[0].src == "") {
                alert("空");
                imglength[0].style.display = "none";
            } else if (imglength[1].src === "") {
                imglength[1].style.display = "none";
            }
        }
    </script>


#img's src is read from the background, and the img is hidden when no image is uploaded in the background.
But in the case of src=" ", the content of the value is the path of the current web page.
How to change this judgment?

ringa_leeringa_lee2663 days ago954

reply all(2)I'll reply

  • 为情所困

    为情所困2017-07-05 10:53:08

    The src attribute value is set to get the current page URL. Location.href is to get the current address. Just add it directly to src

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-05 10:53:08

    Give src a default value and hide it anyway

    reply
    0
  • Cancelreply