0

现在我需要将显示图像更改为图像控件。帮我?

var imlocation1 = "Bhopal/";
var currentdate1 = 0;
var image_number1 = 0;
function ImageArray1(n) {
 this.length = n;
                   for (var i = 1; i <= n; i++) {
                       this[i] = ' '
                   }
               }
               image1 = new ImageArray1(3)
               image1[0] = '2.jpg'
               image1[1] = '4.jpg'
               image1[2] = '1.jpg'
               var rand1 = 100/ image1.length
               function randomimage1() {
                   currentdate1 = new Date()
                   image_number1 = currentdate1.getSeconds()
                   image_number1 = Math.floor(image_number1 / rand1)
                   return (image1[image_number1])
               }
               document.write("<img src='" + imlocation1 + randomimage1() + "'>");
4

3 回答 3

1

“Img”元素具有指向服务器上图像的“src”属性。因此,您需要找到“img”元素,如果找到则设置 src 属性。

整个问题的基本搜索http://www.bing.com/search?q=I+Load+an+Image+to+an+Image+Control+using+JavaScript&src=IE-SearchBox&FORM=IE8SRC给出以下链接:http: //www.javascriptexamples.org/2011/01/18/how-to-dynamically-load-an-image-using-javascript/

来自上面链接的示例:

 <div id="imageContainer"></div>

var img = document.createElement("img");
img.onload = function(e) {
    var container = document.getElementById("imageContainer");
    container.appendChild(e.target);
}
img.setAttribute("src","images/puppy.jpg");
于 2013-01-11T07:33:23.390 回答
0

控件的想法只存在于服务器端。在这种情况下,Javascript 仅在客户端、浏览器上运行。一旦服务器呈现图像控件,您最终会在客户端获得一个 IMG 标记。如果你用 ID 或 CSS 类标记它,你可以用 Javascript(或者更好的库,如 JQuery)找到它,并用它加载图像。

于 2013-01-11T07:30:51.083 回答
0
$("#YourImageId").attr("src", "ImagePath")

If you use webforms - probably

$("#<%=YourImageServerId.ClientID%>")

If it doesn't help - post some code or some more info to your question

于 2013-01-11T07:27:59.247 回答