我正在尝试根据托管服务器的位置创建图像链接。如果服务器是server1。我希望将图像链接到 http://server1/dod “我试图用这张照片获取两张图像。但是,没有加载任何内容。我丢失了一 block ,而且我不知道在哪里。我还将其托管在 IIS 服务器上。
<!DOCTYPE html>
<html>
<body>
<div>
<p id="Images"></p>
</div>
<script>
var local = location.hostname;
//SP Edit, save a reference to the images node
var Images = document.getElementById("Images");
var dodlink = document.createElement(a);
dodlink.href = "http://" + local + "/DoD/";
var dodimg = document.createElement("img");
//SP Edit dodimg.src ="DOD.png";
//SP Edit dodimg.width = 256;
//SP Edit dodimg.height = 256;
dodimg.setAttribute("src", "DOD.png");
dodimg.setAttribute("width", "256px");
dodimg.setAttribute("height", "256px");
dodlink.appendChild(dodimg);
//SP Edit document.getElementByID("Images").innerHTML = dodimg;
Images.appendChild(dodimg);
var derlink = document.createElement(a);
//SP Edit derlink.href = "http://" + local + "/DER/";
deflink.setAttribute("href", "http://" + local + "/DER/");
var derimg = document.createElement("img");
//SP Edit derimg.src ="DER.png";
//SP Edit derimg.width = 256;
//SP Edit derimg.height = 256;
derimg.setAttribute("src", "DER.png");
derimg.setAttribute("width", "256px");
derimg.setAttribute("height", "256px");
deflink.appendChild(derimg);
// Images.appendChild(deflink);
最佳答案
试试这个:
var local = location.hostname;
var dodlink = document.createElement('a');
dodlink.href = "http://" + local + "/DoD/";
var dodimg = document.createElement("img");
dodimg.setAttribute("src", "DOD.png");
dodimg.setAttribute("width", "256");
dodimg.setAttribute("height", "256");
dodlink.appendChild(dodimg);
document.getElementById("Images").appendChild(dodlink);
var derlink = document.createElement('a');
derlink.href = "http://" + local + "/DER/";
var derimg = document.createElement("img");
derimg.setAttribute("src", "DER.png");
derimg.setAttribute("width", "256");
derimg.setAttribute("height", "256");
derlink.appendChild(derimg);
document.getElementById("Images").appendChild(derlink);
关于javascript - 获取本地主机名并使用图像 Javascript 创建链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54237795/