【问题标题】:how to make a javascript randomizer appear images如何使 javascript randomizer 出现图像
【发布时间】:2017-08-30 01:34:49
【问题描述】:

所以我正在尝试制作某种 Web 应用程序(随机字符选择器) 但我不知道如何在不覆盖整个网站的情况下显示图像,对我有什么想法吗?这是我已经拥有的 javascript

function RandomCustoms() {
                            canvasRuimte = document.getElementById('canvas').getContext('2d');
                            canvasRuimte.clearRect(0,0,600,300);
                            canvasRuimte.font = 'italic 30px sans-serif';
                            canvasRuimte.fillStyle = "BLACK";
                            worp1 = Math.floor(24*Math.random())+1;
                            tekenAfbeelding(worp1);
                            worp2 = Math.floor(24*Math.random())+1;
                            tekenAfbeelding(worp2);
                            worp3 = Math.floor(24*Math.random())+1;
                            tekenAfbeelding(worp3);
                            worp4 = Math.floor(24*Math.random())+1;
                            tekenAfbeelding(worp4);
                            worp5 = Math.floor(24*Math.random())+1;
                            tekenAfbeelding(worp5);
                            function tekenAfbeelding(worp1){
                                document.write('<img id="image"src="Paladins-champions/'+worp1+'.png" >');
                            }
                            function tekenAfbeelding(worp2){
                                document.write('<img id="image"src="Paladins-champions/'+worp2+'.png" >');
                            }
                            function tekenAfbeelding(worp3){
                                document.write('<img id="image"src="Paladins-champions/'+worp3+'.png" >');
                            }
                            function tekenAfbeelding(worp4){
                                document.write('<img id="image"src="Paladins-champions/'+worp4+'.png" >');
                            }
                            function tekenAfbeelding(worp5){
                                document.write('<img id="image"src="Paladins-champions/'+worp5+'.png" >');
                            }
                        }

提前致谢

【问题讨论】:

  • 使用document.write() 会导致这个问题。不要使用它。查看 DOM 操作的其余部分。 Image 构造函数和 Node.appendChild 之类的东西

标签: javascript image random


【解决方案1】:

您可以只替换一个元素的 HTML 而不会覆盖您的文档。

在您的 HTML 中,您应该创建一个容器来保存您要显示的图像:

<div id="imgContainer"></div>

然后,您可以使用 javascript 向其中添加图像:

document.getElementById('imgContainer').innerHTML = '<img id="image"src="Paladins-champions/'+worp1+'.png" >';

getElementById 查找具有特定 id 的元素,innerHTML 替换该元素内的 HTML。

您可以在此处查看更多示例和文档:https://www.w3schools.com/js/js_htmldom_html.asp

【讨论】:

  • hm 这样我就可以摆脱所有其他函数编码了?
  • 现在我遇到了下一个问题,图片相互覆盖,我必须将它们放在不同的 div 中吗?
  • 你可以试试 appendChild(),这里是文档https://www.w3schools.com/jsref/met_node_appendchild.asp。您还可以包含 jQuery 库并使用其 append() 方法 http://api.jquery.com/append/。 jQuery 会让你的页面加载时间稍微长一点,但它会让编写代码更容易。
猜你喜欢
  • 2015-09-03
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多