简体   繁体   English

使用数组时Div不会显示

[英]Div will not display when using an array

Trying to randomly generate one div from an array on each load of a page. 尝试在每次页面加载时从数组随机生成一个div。

I've managed to put together bits from a few random image generators to create the below. 我设法将一些随机图像生成器中的位组合在一起以创建以下内容。

When the [0] in the array contains an image, like the rest, everything works as it should. 当数组中的[0]包含图像时,就像其余图像一样,一切都会正常进行。 Once I replace [0]'s image with a div or video, nothing at all displays and the page is blank. 将[0]的图像替换为div或视频后,什么都没有显示,并且页面空白。

Any suggestions where I'm going wrong? 有什么建议我要去哪里吗?

 <link rel="stylesheet" type="text/css" href="style.css" >
     <script type="text/javascript">
         function random()
         {
             video = new Array(4);
             video[0] = "<div class="clip"> </div> ";
             video[1] = "<a href=''><img src='http://lorempixel.com/output/city-q-c-640-480-3.jpg' alt='' /></a>";
             video[2] = "<a href=''><img src='http://lorempixel.com/output/fashion-q-c-640-480-6.jpg' alt='' />";
             video[3] = "<a href=''><img src='http://lorempixel.com/output/technics-q-c-640-480-1.jpg' alt='' />";
             for(var i = 0; i < video.length; i++)
             {
                 index = Math.floor(Math.random() * video.length);
                 document.write(video[index]);
             }
         }
     </script>
 </head>
 <body>
     <script type="text/javascript">
         random();
      </script>
 </body>

You need to escape your quotes. 您需要转义报价。 Try: 尝试:

video[0] = "<div class=\\"clip\\"> </div>";

or use singles: 或使用单打:

video[0] = "<div class='clip'> </div>";

Also, you are aware your div is empty, right? 另外,您知道自己的div是空的,对吗? Unless your style does something to make it display something, it will appear blank. 除非您的样式能够使其显示某些内容,否则它将显示为空白。

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

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