简体   繁体   中英

Using jQuery to read img src and insert img src dynamically

I have some code on my page that looks like this:

<div class="img-center">
    <img src="img-path.jpg" alt="alt-text" class="feature-image" />
    </div>

I am setting up a Print Stylesheet and due to some javascript manipulation of the content (and possibly a bug in firefox) the img prints off center. The solution I am going to do is have the img inserted dynamically into another div that will show when printed but I do not know how to have jQuery read the img div and then copy it into the other div. The code example for the other div would be something like this:

<div id="feature-container-print">
   <img src="jQuery inserted copy of above img-path.jpg" alt="alt-text" />
     </div>

How is this done? Please provide an example.

$('#feature-container-print img').attr('src',$('.feature-image').attr('src'));

I believe this would work:

$('.feature-container-print img').attr('src', $('.img-center img').attr('src'));
$('.img-center img').hide();
$('.feature-container-print img').show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM