javascript - 怎么判斷src的值是否為空?
問題描述
<p class='section benefit-section cftx'><img src='http://www.gepszalag.com/wenda/4390.html' alt='無圖片'><img src='http://www.gepszalag.com/wenda/4390.html' 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的src從后臺讀取,當后臺沒有上傳圖片時將img隱藏。但是src='http://www.gepszalag.com/wenda/ '的情況下值的內(nèi)容為當前網(wǎng)頁的路徑。這個判斷該怎么改?
問題解答
回答1:src屬性值設(shè)置為獲取當前頁面url就可以了,location.href是獲取當前地址 直接添加到src上就可以了
回答2:給src一個默認值,反正隱藏
相關(guān)文章:
1. docker start -a dockername 老是卡住,什么情況?2. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?3. docker gitlab 如何git clone?4. dockerfile - [docker build image失敗- npm install]5. docker不顯示端口映射呢?6. docker-compose 為何找不到配置文件?7. docker內(nèi)創(chuàng)建jenkins訪問另一個容器下的服務(wù)器問題8. css - 百度Ueditor富文本編輯器初始化賦值font-size沒有值?9. Ant design vue中的聯(lián)動選擇取消操作10. javascript - js 多維數(shù)組的問題
