如何在一个函数中附加两份相同图像的副本?

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

How can I append two copies of the same image in one function?

问题

这是一段JS代码片段,一直让我遇到问题。我试图添加两个带有不同动态生成HTML标签的图像层,但浏览器拒绝正确渲染代码。
期望的结果是

dropzone.append(img);
dropzone.append(imgIn);

添加两个不同索引的图像,并在拼图中重复数百次。
这是此代码用于的Android应用程序的链接 https://www.webintoapp.com/store/107768

function hueChange() {
  for(let image = 0; image < mosaicCount; image++) {
    let screenWidth = window.innerWidth;
    reader.addEventListener('loadend', () => {
      let img = document.createElement('img');
      let imgIn = document.createElement('imgIn');
      img.setAttribute("id", image)
      img.setAttribute("width", screenWidth/16 + "px")
      imgIn.setAttribute("id", "imgIn" + image)
      imgIn.setAttribute("width", screenWidth/16 + "px")
      img.src = reader.result;
      imgIn.src = reader.result;

      dropzone.append(img);   //这不会附加
      dropzone.append(imgIn); //这会附加
      });
    }
  }
英文:

this is a js snippet of code that's been giving me problems. I'm attempting to append 2 img layers with different dynamically generated HTML tags but browsers refuse to render the code correctly.
the desired outcome is for

dropzone.append(img);
dropzone.append(imgIn);

append 2 images with different 2 indexes and do this hundreds of times in a mosaic
here is a link to the android app this code is for https://www.webintoapp.com/store/107768

function hueChange() {
  for(let image = 0; image &lt; mosaicCount; image++) {
    let screenWidth = window.innerWidth;
    reader.addEventListener(&#39;loadend&#39;, () =&gt; {
      let img = document.createElement(&#39;img&#39;);
      let imgIn = document.createElement(&#39;imgIn&#39;);
      img.setAttribute(&quot;id&quot;, image)
      img.setAttribute(&quot;width&quot;, screenWidth/16 + &quot;px&quot;)
      imgIn.setAttribute(&quot;id&quot;, &quot;imgIn&quot; + image)
      imgIn.setAttribute(&quot;width&quot;, screenWidth/16 + &quot;px&quot;)
      img.src = reader.result;
      imgIn.src = reader.result;

      dropzone.append(img);   //this doesn&#39;t append
      dropzone.append(imgIn); //this appends

答案1

得分: 1

你可以创建一个元素,并在附加时克隆它。然后更新原始元素并附加它。

const img = document.createElement('img');
const dropzone = document.querySelector('#dropzone');
const imageId = 'imageId_';

img.setAttribute("id", imageId + '1');
img.setAttribute("width", '100px');
img.src = 'https://via.placeholder.com/100';
dropzone.append(img.cloneNode());

img.setAttribute("id", imageId + '2');
img.setAttribute("width", '100px');
img.src = 'https://via.placeholder.com/100/ff0000';
dropzone.append(img);
<div id="dropzone"></div>
英文:

You could create just one element and clone it when you append. Then update the original element and append that.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const img = document.createElement(&#39;img&#39;);
const dropzone = document.querySelector(&#39;#dropzone&#39;);
const imageId = &#39;imageId_&#39;;

img.setAttribute(&quot;id&quot;, imageId + &#39;1&#39;);
img.setAttribute(&quot;width&quot;, &#39;100px&#39;); // screenWidth / 16 + &quot;px&quot;);
img.src = &#39;https://via.placeholder.com/100&#39;; // reader.result;
dropzone.append(img.cloneNode());

img.setAttribute(&quot;id&quot;, imageId + &#39;2&#39;);
img.setAttribute(&quot;width&quot;, &#39;100px&#39;); // screenWidth / 16 + &quot;px&quot;);
img.src = &#39;https://via.placeholder.com/100/ff0000&#39;; // reader.result;
dropzone.append(img);

<!-- language: lang-html -->

&lt;div id=&quot;dropzone&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

不存在名为`imgIn`的元素,所以该元素不会渲染。你应该对两个元素都使用`img`。

应该将循环放在事件侦听器内,而不是添加多个执行相同操作的侦听器。
英文:

There's no such thing as an imgIn element, so that element doesn't render. You should use img for both elements.

You should put the loop inside the event listener, instead of adding multiple listeners that all do the same thing.

function hueChange() {
  reader.addEventListener(&#39;loadend&#39;, () =&gt; {
    let imgWidth = window.innerWidth / 16 + &quot;px&quot;;

    for (let image = 0; image &lt; mosaicCount; image++) {
      let img = document.createElement(&#39;img&#39;);
      let imgIn = document.createElement(&#39;img&#39;);
      img.setAttribute(&quot;id&quot;, image)
      img.setAttribute(&quot;width&quot;, imgWidth)
      imgIn.setAttribute(&quot;id&quot;, &quot;imgIn&quot; + image)
      imgIn.setAttribute(&quot;width&quot;, imgWidth)
      img.src = reader.result;
      imgIn.src = reader.result;

      dropzone.append(img);
      dropzone.append(imgIn);
    }
  });
}

答案3

得分: 0

首先的问题是你创建了一个"custom"元素,因为你创建了一个带有"id"为"imgIn"的元素,所以不会呈现图像。

你可以使用cloneNode并更改id,这样就不必重复id。

const dropzone = document.querySelector('#dropZone');

const screenWidth = window.innerWidth;
const image = 1;

const img = document.createElement('img');
img.setAttribute("id", 'img' + image);
img.setAttribute("width", screenWidth / 16 + "px");
img.src = 'http://placekitten.com/200/300'; // reader.result;
dropzone.append(img);

const imgIn = img.cloneNode(true);
imgIn.setAttribute("id", 'imgIn' + image);
dropzone.append(imgIn);
<div id="dropZone"></div>

<details>
<summary>英文:</summary>

Firs problem is you are creating a &quot;custom&quot; element since you are creating an element with &quot;imgIn&quot; so that is not going to render and image.

You can use [cloneNode][1] and alter the id so you do not have to duplicate id.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    const dropzone = document.querySelector(&#39;#dropZone&#39;);

    const screenWidth = window.innerWidth;
    const image = 1;

    const img = document.createElement(&#39;img&#39;);
    img.setAttribute(&quot;id&quot;, &#39;img&#39; + image)
    img.setAttribute(&quot;width&quot;, screenWidth / 16 + &quot;px&quot;)
    img.src = &#39;http://placekitten.com/200/300&#39;; // reader.result;
    dropzone.append(img);

    const imgIn = img.cloneNode(true);
    imgIn.setAttribute(&quot;id&quot;, &#39;imgIn&#39; + image)
    dropzone.append(imgIn);

&lt;!-- language: lang-html --&gt;

    &lt;div id=&quot;dropZone&quot;&gt;&lt;/div&gt;

&lt;!-- end snippet --&gt;


  [1]: https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode

</details>



huangapple
  • 本文由 发表于 2023年4月11日 03:17:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980033.html
匿名

发表评论

匿名网友

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

确定