Adding image src in the inline html under javascript

Hi I am showing a infoWindow on click of the marker on google map , the content of the infoWindow is an inline html along with img tag the src of the image changes for every marker,I want to get the img src dynamicaly the code what iv done is

var contentString = '<div id="content">'+
  '<div id="siteNotice">'+
  '<img id="image" alt="No Photograph to Display" src="" width="400px" height="250px" />'+

  '</div>'+
  '<h1 id="firstHeading" class="firstHeading">'+title+'</h1>'+
  '<div id="bodyContent">'+
  '<p>'+probdesc+'</p><br>'+
  '<p><b>Open Date:</b>'+opendate+'</p><br>'+
   '<p><b>Status:</b>'+status+'</p><br>'+
  '</div>'+
  '</div>';

the image tag must get the src value must be replaced by url value which contains the dynamic path of the image

var url = '<%=imageURL %>'+ "&<portlet:namespace/>imagepath=" + imagepath;

You can use jQuery for it, after print the HTML code on your page:

$("#image").attr("src", url); //"url" is your var.

Only Javascript:

document.getElementById("image").setAttribute("src", url);
document.getElementById("image").src = url; //it works too - http://www.w3schools.com/js/js_htmldom_html.asp

Just for make it simple, after Paul S' comment. If you don't print the HTML on your page, the "attr" code wouldn't work. For print the HTML code on your page, you can use:

$("#anchor_element").html(contentString);
// and then
$("#image") .....

Using pure Javascript:

document.getElementById("anchor").innerHTML = contentString;
// and then
document.getElementById("image")......
链接地址: http://www.djcxy.com/p/42164.html

上一篇: 使用Javascript中的URL来源从Img标记获取框架

下一篇: 在javascript下的inline html中添加图像src