提前谢谢您。当 img 标签的 src 属性中有内容时,我需要显示文本。我需要一些 JS 来检测属性是否有内容,如果有,则 p 标签可见。
html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type='text/javascript' src="SampleJS.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p id="sponsorText">Now you see me!<img src="" class="sponsorImg" id="sponsorText" /></p>
</body>
</html>
js
var attr = $().attr('src');
if (typeof attr !== "" && attr !== false){
alert('no image.');
}
但此时我什至无法让 JS 检测并发出警报。
我需要 Jscript 来检测是否存在 filename.jpeg,如果存在则触发文本。
最佳答案
首先包含 jQuery。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
其次,创建脚本为
$(document).ready(function () {
// code here..
});
第三,您需要提供一个可以使用的选择器。
var attr = $('img').attr('src'); // add img element
现在就可以了。
关于javascript - 我需要根据是否有图像显示/隐藏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272748/