Programing

jQuery는 div 태그 안에 이미지를 추가합니다.

lottogame 2020. 6. 12. 22:09
반응형

jQuery는 div 태그 안에 이미지를 추가합니다.


div 태그가 있습니다

<div id="theDiv">Where is the image?</div>

div 안에 이미지 태그를 추가하고 싶습니다

최종 결과 :

<div id="theDiv"><img id="theImg"  src="theImg.png" />Where is the image?</div>

다음을 시도 했습니까?

$('#theDiv').prepend('<img id="theImg" src="theImg.png" />')

내 2 센트 :

$('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))

$("#theDiv").append("<img id='theImg' src='theImg.png'/>");

여기 에서 설명서를 읽어야합니다 .


<div>함수 image()가 호출 될 때마다 태그 의 내용을 변경하려면 다음과 같이해야합니다.

자바 스크립트

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>

Manjeet Kumar의 게시물 외에도 (그는 선언이 없었습니다)

var image = document.createElement("IMG");
image.alt = "Alt information for image";
image.setAttribute('class', 'photo');
image.src="/images/abc.jpg";
$(#TheDiv).html(image);

var img;
for (var i = 0; i < jQuery('.MulImage').length; i++) {
                var imgsrc = jQuery('.MulImage')[i];
                var CurrentImgSrc = imgsrc.src;
                img = jQuery('<img class="dynamic" style="width:100%;">');
                img.attr('src', CurrentImgSrc);
                jQuery('.YourDivClass').append(img);

            }

참고 URL : https://stackoverflow.com/questions/941206/jquery-add-image-inside-of-div-tag

반응형