我有以下脚本没有给我想要的结果。底部的JSFiddle链接。
注意:这不是我的照片。我从imgur中随机地抓起它。
我希望:
<img src="https://i.imgur.com/MiOeWrk.jpg" style="max-width:800px;">
但是,我相信我能理解,但是由于某种原因,我在调试时无法在代码块中编写它。
<img src="http://thisismydomain.com/https://i.imgur.com/MiOeWrk.jpg" style="max-width:800px;">
这是我的剧本。请告知:
<div><a href="javascript:viewimage('https://i.imgur.com/MiOeWrk.jpg','video_aba');">Show Image</a></div>
<div id="jsdebug">jsdebug</div>
<div id="video_aba">video_aba</div>
<script>
function viewimage(ytlink, uid) {
var imagelink = ytlink;
document.getElementById("jsdebug").innerHTML = imagelink;
var uid = uid;
if (imagelink.length == 0) {
imageembed = "<strong>Sorry, unable to load.</strong>";
} else {
imageembed = "<img src=\"/" + imagelink + "\" style=\"max-width:800px\">";
}
document.getElementById("video_aba").innerHTML = imageembed;
}
</script>
答案 0 :(得分:1)
您的出处不正确。 href
应该替换为onclick
事件。
onclick
,onhover
,onmouseenter
等。
function viewimage(ytlink, uid) {
var imagelink = ytlink;
var uid = uid;
if (imagelink.length == 0) {
imageembed = "<strong>Sorry, unable to load.</strong>";
} else {
imageembed = `<img src="${ytlink}" style="width:800px">`;
}
document.getElementById("video_aba").innerHTML = imageembed;
document.getElementById("jsdebug").innerText = imageembed;
}
a {
color: blue;
text-decoration: underline;
cursor: pointer;
}
<div><a href="#" onclick="viewimage('https://i.imgur.com/MiOeWrk.jpg','video_aba');">Show Image</a></div>
<div id="jsdebug">jsdebug</div>
<div id="video_aba">video_aba</div>
答案 1 :(得分:1)
问题是您的图片链接编码了什么。就像这样的imageembed = "<img src=\"/" + imagelink + "\" style=\"max-width:800px\">";
/ =>这将添加到您当前的域URL,例如,如果您的域是localhost,它将变为localhost / https://i.imgur.com/MiOeWrk.jpg
因此,从图像链接中删除/