获取JavaScript图像的SRC。

huangapple go评论59阅读模式
英文:

Get SRC of Javascript image

问题

Here's the translated content you requested without code translation:

I am creating an image gallery in javascript and I wrote some functions for it. When I click the "add" button, a window opens to select a file, and an "" tag is created in my relevant container. But how do I automatically put the src of the SELECTED image in my img tag?

My codes:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@1&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="style.css">
    <title>Gallery</title>
</head>
<body>
    <div class="container">
        <h1>Gallery</h1>
        <button id="addButton">Add</button>
        <div id="containerImage" class="container-image"></div>
    </div>
    <script src="app.js"></script>
</body>
</html>
let addButton = document.querySelector("#addButton");
let imageContainer = document.querySelector("#containerImage");

addButton.addEventListener('click', function () {
    let input = document.createElement('input');
    input.type = "file";
    input.click();
});

addButton.addEventListener('click', function () {
    let image = document.createElement('img');
    imageContainer.appendChild(image);
    image.className = "resim";
});

I'm new to javascript and I'm trying to do small projects.

I tried:

image.src = URL.createObjectURL(image)

Please note that your code snippet image.src = URL.createObjectURL(image) seems to have a problem. You should use the selected file to create a URL instead of image. You might want to use the input element to get the selected file and then create a URL for it.

英文:

I am creating an image gallery in javascript and I wrote some functions for it. When I click the "add" button, a window opens to select a file and a "&lt;img&gt;" tag is created in my relevant container, but how do I automatically put the src of the SELECTED image in my img tag?

my codes;

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.googleapis.com&quot;&gt;
    &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot; crossorigin&gt;
    &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Roboto+Mono:ital@1&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;title&gt;Gallery&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;h1&gt;Gallery&lt;/h1&gt;
        &lt;button id=&quot;addButton&quot;&gt;Add&lt;/button&gt;
        &lt;div id=&quot;containerImage&quot; class=&quot;container-image&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

    &lt;script src=&quot;app.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;
let addButton = document.querySelector(&quot;#addButton&quot;);
let imageContainer = document.querySelector(&quot;#containerImage&quot;);


addButton.addEventListener(&#39;click&#39;, function () {
    let input = document.createElement(&#39;input&#39;);
    input.type = &quot;file&quot;;
    input.click();
});

addButton.addEventListener(&#39;click&#39;,function(){
    let image = document.createElement(&#39;img&#39;);
    imageContainer.appendChild(image);
    image.className=&quot;resim&quot;;
    
});

Im new to javascript and I'm trying to do small small projects

i tried ;

image.src = URL.createObjectURL(image)

答案1

得分: 1

addButton.addEventListener('click', function () {
  let input = document.createElement('input');
  input.type = "file";
  input.addEventListener('change', function () {
    let image = document.createElement('img');
    image.className = "resim";
    image.src = URL.createObjectURL(input.files[0]);
    imageContainer.appendChild(image);
  });
  input.click();
});
英文:
addButton.addEventListener(&#39;click&#39;, function () {
  let input = document.createElement(&#39;input&#39;);
  input.type = &quot;file&quot;;
  input.addEventListener(&#39;change&#39;, function () {
    let image = document.createElement(&#39;img&#39;);
    image.className = &quot;resim&quot;;
    image.src = URL.createObjectURL(input.files[0]);
    imageContainer.appendChild(image);
  });
  input.click();
});

huangapple
  • 本文由 发表于 2023年2月14日 19:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定