-1

嘿伙计们,所以我试图将 .png 类型的图片作为参数传递给函数。所以每次我用图片参数调用函数时,图片都会显示出来。这是我的功能

    function DisplayImages(picture1){
     document.write("<div class='slideshow'>",
      "<ul>",
        "<li>",
         "<img src=",picture1,"/>",          
          "<div>",
             "<a href=", picture1 "title='Click to see full image' target='_blank'>",
                "<img src=", picture1 " alt='Item 1' />",
            "</a>",
            "<p>'This is Item 1' </p>",
        "</div>",
        "</ul>",
       "</div>");
    }

我这样称呼它

    <script> DisplayImages('images/upcomingitem1.png'); </script>

但它没有显示任何东西,任何帮助如何解决这个问题?

4

2 回答 2

1

如果添加两个缺少的逗号,第二个之后缺少的空格src和缺少的</li>,它“有效”:

function DisplayImages(picture1){
     document.write("<div class='slideshow'>",
      "<ul>",
        "<li>",
         "<img src=\"", picture1,"\">",          
          "<div>",
             "<a href=\"", picture1, "\" title='Click to see full image' target='_blank'>",
                "<img src=\"", picture1, "\" alt='Item 1' />",
            "</a>",
            "<p>'This is Item 1' </p>",
        "</div></li>",
        "</ul>",
       "</div>");
}

我还添加了一些(转义)引号,因此它适用于任何 URL。

示范

但我对解决方案持怀疑态度。整棵树看起来很重。

于 2012-10-21T19:22:03.310 回答
0

正确格式化您的字符串以写入,+连接,而不是,

 function DisplayImages(picture1){
 document.write("<div class='slideshow'>"+
  "<ul>"+
    "<li>"+
     "<img src="+picture1+"/>"+          
      "<div>"+
         "<a href="+ picture1 +"title='Click to see full image' target='_blank'>"+
            "<img src="+ picture1 +" alt='Item 1' />"+
        "</a>"+
        "<p>This is Item 1 </p>"+
    "</div>"+
    "</ul>"+
   "</div>");
 }
于 2012-10-21T19:19:30.120 回答