jQuery add image inside of div tag

前端 未结 6 2219
北荒
北荒 2020-11-29 18:02

I have a div tag

Where is the image?

I would like to add an image tag inside of the div

End re

相关标签:
6条回答
  • 2020-11-29 18:24
    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);
    
                }
    
    0 讨论(0)
  • 2020-11-29 18:30

    my 2 cents:

    $('#theDiv').prepend($('<img>',{id:'theImg',src:'theImg.png'}))
    
    0 讨论(0)
  • 2020-11-29 18:35

    In addition to Manjeet Kumar's post (he didn't have the declaration)

    var image = document.createElement("IMG");
    image.alt = "Alt information for image";
    image.setAttribute('class', 'photo');
    image.src="/images/abc.jpg";
    $(#TheDiv).html(image);
    
    0 讨论(0)
  • 2020-11-29 18:43
    $("#theDiv").append("<img id='theImg' src='theImg.png'/>");
    

    You need to read the documentation here.

    0 讨论(0)
  • 2020-11-29 18:44

    If we want to change the content of <div> tag whenever the function image()is called, we have to do like this:

    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>
    
    0 讨论(0)
  • 2020-11-29 18:45

    Have you tried the following:

    $('#theDiv').prepend('<img id="theImg" src="theImg.png" />')
    
    0 讨论(0)
提交回复
热议问题