2015-04-08 86 views
2

我想单击按钮时出现图片。我认为我已经在代码中获得了一切,但我需要图像链接方面的帮助。Javascript图片链接

我想从网站上拍照。我在这段代码中添加了什么代码来完成它?

<script type = "text/javascript"> 
    function pic1() 
    { 
     document.getElementById("img").src ="; 
    } 

<img src = "" id = "img"/> <input type="button" value="Click for image!" onclick="pic1()"/> 
+0

“SRC” 是一个属性,这样你element.setAttribute设置()看到这里的https:/ /developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute – Dinesh

+0

啊,是的,它应该。 –

+0

您只需在现有代码中输入网址即可。例如:'document.getElementById(“img”)。src ='https://www.google.com/images/srpr/logo11w.png';'。 http://jsfiddle.net/j08691/tof3yy8b/ – j08691

回答

0

试试这个:

function pic1() 
 
{ 
 
    //set this to the url of whatever image you want to appear 
 
    var imageUrl = "https://www.google.com/images/srpr/logo11w.png"; 
 
    document.getElementById("img").setAttribute("src",imageUrl); 
 
}
<img src="" id="img"/> 
 
<input type="button" value="Click for image!" onclick="pic1()"/>