javascript - 怎么判断src的值是否为空?
问题描述
<p class='section benefit-section cftx'><img src='https://www.haobala.com/wenda/4390.html' alt='无图片'><img src='https://www.haobala.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='https://www.haobala.com/wenda/ '的情况下值的内容为当前网页的路径。这个判断该怎么改?
问题解答
回答1:src属性值设置为获取当前页面url就可以了,location.href是获取当前地址 直接添加到src上就可以了
回答2:给src一个默认值,反正隐藏
相关文章:
1. 模板渲染时报错,怎么处理2. Java。比较字符串时忽略重音3. node.js - 使用mongoose的cursor的问题4. javascript - nodemailer连接超时,怎么解决?5. PHP中的$this代表当前的类还是方法?6. mysql - Access denied for user ’ODBC’@’localhost’ (using password: NO)7. 请问一下,图片上传成功,但是后台对应文件夹里面却没有图片,这是什么原因?(已部署到服务器)8. 老师,flex-shrink: 1; 按视频操作,不会自动缩放9. mysql单项同步10. php - MySQL数据库设计,获取点赞的人数

网公网安备