Mam znacznik DIV
<div id="theDiv">Where is the image?</div>
Chciałbym dodać tag obrazu wewnątrz elementu div
Wynik końcowy:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
Mam znacznik DIV
<div id="theDiv">Where is the image?</div>
Chciałbym dodać tag obrazu wewnątrz elementu div
Wynik końcowy:
<div id="theDiv"><img id="theImg" src="theImg.png" />Where is the image?</div>
Odpowiedzi:
Czy próbowałeś następujących rzeczy:
$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')
moje 2 centy:
$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))
$("#theDiv").append("<img id='theImg' src='theImg.png'/>");
Musisz przeczytać dokumentację tutaj .
Jeśli chcemy zmienić zawartość <div>tagu przy każdym wywołaniu funkcji image(), musimy zrobić to w następujący sposób:
Javascript
function image() {
var img = document.createElement("IMG");
img.src = "/images/img1.gif";
$('#image').html(img);
}
HTML
<div id="image"></div>
<div><a href="javascript:image();">First Image</a></div>
Oprócz stanowiska Manjeeta Kumara (nie miał deklaracji)
var image = document.createElement("IMG");
image.alt = "Alt information for image";
image.setAttribute('class', 'photo');
image.src="/images/abc.jpg";
$(#TheDiv).html(image);