Input to each letter’s own individual image HTML 将每个字母都转换为独立的图像HTML

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

Input to each letter's own individual image HTML

问题

I am making a HTML website that takes an input in the id userInput and was wondering how to take it apart and insert images for the length of userInput

Say for example the userInput = Hello World
Ideally it would output the images h.png e.png l.png l.png o.png space.png w.png o.png r.png l.png d.png

As I do not have the files of each individual letter, I am unable to show how it would ideally look, but hopefully you can help me! Though I know that the font disregards capitals.

Thank you once more!

英文:

I am making a HTML website that takes an input in the id userInput and was wondering how to take it apart and insert images for the length of userInput

Say for example the userInput = Hello World
Ideally it would out put the images h.png e.png l.png l.png o.png space.png w.png o.png r.png l.png d.png

As I do not have the files of each individual letter, I am unable to show how it would ideally look, but hopefully you can help me! Though I know that the font disregards capitals.

Thank you once more!

答案1

得分: 1

你只需要编写一些JavaScript代码来将输入字符串转换为数组,然后遍历该数组,创建一个包含<img>标签的HTML字符串。

document.querySelector('input').addEventListener('input', evt => {
  document.querySelector('.normal-output').innerHTML = evt.target.value;
  const a1 = Array.from(evt.target.value);
  const a2 = [];
  a1.forEach(e1 => {
    e2 = (e1 == ' ' ? 'space' : e1);
    a2.push(`<img src="https://donald.au/bugs/so-76709587/${e2}.png">`);
  });
  document.querySelector('.image-output').innerHTML = a2.join('');
});
body, input {font-family: sans-serif; font-size: 16px;}
.image-output img {height: 100px;}
<p>
  <input type="text" value="abc">
</p>
<p class="normal-output">abc</p>
<p class="image-output">
  <img src="https://donald.au/bugs/so-76709587/a.png">
  <img src="https://donald.au/bugs/so-76709587/b.png">
  <img src="https://donald.au/bugs/so-76709587/c.png">
</p>
英文:

You just need to write some Javascript to take your input string, convert it to an array, and iterate through the array, creating an HTML string containing the &lt;img&gt; tags.

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

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

document.querySelector(&#39;input&#39;).addEventListener(&#39;input&#39;, evt =&gt; {
  document.querySelector(&#39;.normal-output&#39;).innerHTML = evt.target.value
  const a1 = Array.from(evt.target.value)
  const a2 = []
  a1.forEach(e1 =&gt; {
    e2 = (e1 == &#39; &#39; ? &#39;space&#39; : e1)
    a2.push(`&lt;img src=&quot;https://donald.au/bugs/so-76709587/${e2}.png&quot;&gt;`)
  })
  document.querySelector(&#39;.image-output&#39;).innerHTML = a2.join(&#39;&#39;)
})

<!-- language: lang-css -->

body, input {font-family: sans-serif; font-size: 16px;}
.image-output img {height: 100px;}

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

&lt;p&gt;
  &lt;input type=&quot;text&quot; value=&quot;abc&quot;&gt;
&lt;/p&gt;
&lt;p class=&quot;normal-output&quot;&gt;abc&lt;/p&gt;
&lt;p class=&quot;image-output&quot;&gt;
  &lt;img src=&quot;https://donald.au/bugs/so-76709587/a.png&quot;&gt;
  &lt;img src=&quot;https://donald.au/bugs/so-76709587/b.png&quot;&gt;
  &lt;img src=&quot;https://donald.au/bugs/so-76709587/c.png&quot;&gt;
&lt;/p&gt;

<!-- end snippet -->

答案2

得分: 0

我有一个解决方案

HTML

  &lt;button onclick=&quot;generateImages()&quot;&gt;生成图像&lt;/button&gt;
  &lt;div id=&quot;imageOutput&quot;&gt;&lt;/div&gt;

JS

function generateImages() {
  var userInput = document.getElementById(&#39;userInput&#39;).value;
  var imageOutput = document.getElementById(&#39;imageOutput&#39;);
  imageOutput.innerHTML = &#39;&#39;;

  for (var i = 0; i &lt; userInput.length; i++) {
    var character = userInput[i].toUpperCase();
    var image = document.createElement(&#39;img&#39;);
    image.src = character === &#39; &#39; ? &#39;FONT/SPACE.png&#39; : &#39;FONT/&#39; + character + &#39;.png&#39;;
    imageOutput.appendChild(image);
  }
}
```

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

I have a solution

HTML
```  &lt;input type=&quot;text&quot; id=&quot;userInput&quot; placeholder=&quot;Enter text&quot; /&gt;
  &lt;button onclick=&quot;generateImages()&quot;&gt;Generate Images&lt;/button&gt;
  &lt;div id=&quot;imageOutput&quot;&gt;&lt;/div&gt;
```

JS
```
function generateImages() {
  var userInput = document.getElementById(&#39;userInput&#39;).value;
  var imageOutput = document.getElementById(&#39;imageOutput&#39;);
  imageOutput.innerHTML = &#39;&#39;;

  for (var i = 0; i &lt; userInput.length; i++) {
    var character = userInput[i].toUpperCase();
    var image = document.createElement(&#39;img&#39;);
    image.src = character === &#39; &#39; ? &#39;FONT/SPACE.png&#39; : &#39;FONT/&#39; + character + &#39;.png&#39;;
    imageOutput.appendChild(image);
  }
}
```

</details>



# 答案3
**得分**: -1

要在HTML中将每个字母显示为其各自的图像,您可以为每个字母使用“img”标签,并将“src”属性设置为相应图像的URL。

在此示例中,每个字母(A、B、C)将由其各自的图像表示。请将“src”属性中的图像URL替换为您要用于每个字母的实际URL。

请确保通过提供的URL或相对路径访问图像文件,如果您在本地托管图像。此外,考虑为每个图像使用适当的替代文本(“alt”属性),以确保可访问性并提高SEO。

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

To display each letter as its individual image in HTML, you can use the &quot;img&quot; tag for each letter and set the &quot;src&quot; attribute to the URL of the corresponding image.

In this example, each letter (A, B, C) will be represented by its respective image. Replace the image URLs in the &quot;src&quot; attribute with the actual URLs of the images you want to use for each letter.

Remember to make sure that the image files are accessible via the provided URLs or the relative paths if you&#39;re hosting the images locally. Additionally, consider using appropriate alternative text (&quot;alt&quot; attribute) for each image to ensure accessibility and improve SEO.

</details>



huangapple
  • 本文由 发表于 2023年7月18日 12:43:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709587.html
匿名

发表评论

匿名网友

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

确定