最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Set src attribute of img tag with javascript - Stack Overflow

programmeradmin4浏览0评论

I have the following elements in a html document

<div id="u11" class="ax_default image" data-label="image1">
    <img id="u11_img" class="img " src="images/image1_u11.png">
</div>

<div id="u23" class="ax_default image" data-label="image2">
    <img id="u23_img" class="img " src="images/image2_u23.png">
</div>

I want to write a javascript onclick function for another element that set image2 src to image1 src

The problem is that ids are random so I cannot use them but fortunately I can use the data-label to get the external div objects with $('[data-label=image1]')

¿How can I set the src in the inner img?

I have the following elements in a html document

<div id="u11" class="ax_default image" data-label="image1">
    <img id="u11_img" class="img " src="images/image1_u11.png">
</div>

<div id="u23" class="ax_default image" data-label="image2">
    <img id="u23_img" class="img " src="images/image2_u23.png">
</div>

I want to write a javascript onclick function for another element that set image2 src to image1 src

The problem is that ids are random so I cannot use them but fortunately I can use the data-label to get the external div objects with $('[data-label=image1]')

¿How can I set the src in the inner img?

Share edited Oct 21, 2017 at 21:08 M0ns1f 2,7253 gold badges17 silver badges25 bronze badges asked Oct 21, 2017 at 19:22 user8812179user8812179 431 gold badge1 silver badge3 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 1

You can use the following

var image1 = document.querySelector('[data-label="image1"] img'),
    image2 = document.querySelector('[data-label="image2"] img');

document.querySelector('#button').addEventListener('click', function(){
    image2.src = image1.src;
});

here is an example using each function :

 
$( "#change" ).click(function() {
   // get all image element in body
    var selects = $('body').find('img');
    // loop throw them
    selects.each(function(index, el) {
        if(index !== selects.length - 1) {
           //save the img src to local variable 
            var img1 = selects[index].getAttribute('src');
            var img2 = selects[index+1].getAttribute('src');
           // change the imgs src
             selects[index].src = img2;
             selects[index+1].src = img1;
          // and have a nice day ^^'
        }
    });
});
img{
 max-width:95px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="u11" class="ax_default image" data-label="image1">
    <img id="u11_img" class="img " src="https://media-cdn.tripadvisor./media/photo-s/0e/4c/55/8d/merzouga.jpg">
</div>

<div id="u23" class="ax_default image" data-label="image2">
    <img id="u23_img" class="img " src="http://www.classetouriste.be/wp-content/uploads/2012/11/sahara-01.jpg">
</div>

<button id="change">Change images</button>

this code will work no matter how many img you have in your body or you can just set the element that contain the image var selects $('#yourelementid').find('img');

You could change the entire code with the src included.

function myFunction() {
 document.getElementById("u11").innerHTML = '<img id="u23_img" class="img " src="images/image2_u23.png">';
 }
}
<div id="u11" class="ax_default image" data-label="image1">
    <img id="u11_img" class="img " src="images/image1_u11.png">
</div>

<div id="u23" class="ax_default image" data-label="image2">
    <img id="u23_img" class="img " src="images/image2_u23.png">
</div>

发布评论

评论列表(0)

  1. 暂无评论