Javascript将图片传递给函数作为参数

时间:2012-10-21 19:15:39

标签: javascript html

嘿伙计们,所以我试图将.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>

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

2 个答案:

答案 0 :(得分:1)

如果你添加了两个缺失的逗号,第二个src之后缺少的空格,以及缺少的</li>,它就会起作用#34; :

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>");
}

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

DEMONSTRATION

但我对这个解决方案表示怀疑。整棵树似乎很重。

答案 1 :(得分: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>");
 }