简体   繁体   English

将输入URL提取到img scr

[英]Fetch input URL to img scr

i am trying to get the URL from the input field to the SRC of img tag. 我正在尝试从输入字段到img标签的SRC获取URL。

<input id="load"> // here we type the URL of image
<input type="submit">
  <img id="my-image" onclick="doit()" src='' />

 function doit() { // get the value of the input const {value} = document.getElementById('load'); // set the src on the img document.getElementById('my-image').src = value; } 
 img { display: block; } input { min-width: 200px; } 
 <input id="load" value="http://placekitten.com/200" /> <button onClick="doit()">go</button> <img id="my-image" src="" /> 

Move your function up to the submit button rather than the image. 将功能上移至“提交”按钮,而不是图像。

 function doit(){ // grab the url from the input var url = document.getElementById("load").value; // load the image from the input url document.getElementById("my-image").src = url; } 
 <input id="load"> // here we type the URL of image <input type="submit" onclick="doit()"> <img id="my-image" src='' /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM