从另一个DIV在DIV中调用Javascript函数

时间:2022-04-03 23:58:00

I have a javascript function displayImages() to display all the images in a directory. I am calling this function inside a DIV "photo_gallery" so that the images display in that DIV only (after page loads)

我有一个javascript函数displayImages()来显示目录中的所有图像。我在DIV“photo_gallery”中调用此函数,以便图像仅显示在该DIV中(页面加载后)

However I also needs to display the image gallery dynamically and for which I have to call this function from outside DIV i:e from other function inside my js file.

但是我还需要动态显示图像库,我必须从外部DIV i调用此函数:来自我的js文件中的其他函数。

<div id="photo_gallery" style="display:none">
  <ul>
    <script>displayImages();</script>
</ul>

</div>

Can someone please advise

有人可以建议

Below is my function :

以下是我的功能:

Note : (here images is an array of images)

注意:(这里的图像是一个图像数组)

function displayImages() {

 for (i=0;i<images.length;i++) {

  document.write("<a href='#pop1'>");

   document.write("<img onclick='currentimage(&quot;" + images[i] + "&quot;)'    class='photo'  src='" + images[i] + "' width='160' height='120'/>");
 document.write("</a>");


}
}

1 个解决方案

#1


1  

You have to execute the javascript-function in the body onload event or in a script tag outside your div-container.

您必须在body onload事件或div-container外部的脚本标记中执行javascript-function。

<body onLoad="displayImages()">

In your function displayImages() you have to set the innerHTML (content) of the Div-Container f.ex.:

在你的函数displayImages()中你必须设置Div-Container f.ex的innerHTML(内容):

yourHtmlGalleryGeneratedContent = '';
for (i=0;i<images.length;i++) {
 yourHtmlGalleryGeneratedContent += "<a href='#pop1'>";
 [...]
}
document.getElementById('photo_gallery').innerHTML = yourHtmlGalleryGeneratedContent;

#1


1  

You have to execute the javascript-function in the body onload event or in a script tag outside your div-container.

您必须在body onload事件或div-container外部的脚本标记中执行javascript-function。

<body onLoad="displayImages()">

In your function displayImages() you have to set the innerHTML (content) of the Div-Container f.ex.:

在你的函数displayImages()中你必须设置Div-Container f.ex的innerHTML(内容):

yourHtmlGalleryGeneratedContent = '';
for (i=0;i<images.length;i++) {
 yourHtmlGalleryGeneratedContent += "<a href='#pop1'>";
 [...]
}
document.getElementById('photo_gallery').innerHTML = yourHtmlGalleryGeneratedContent;