简体   繁体   中英

How do I change data-magnify-src attribute of HTML element with jquery?

I've tried

$(elem).prop('data-magnify-src', 'change')

$(elem).data('magnify-src', 'change')

This is my snippet. I hope I am not doing something dumb. I really thought it would work.

  $().ready(function () { $('.img-miniatura').click(function(e){ $('#bigImg').prop('data-magnify-src', 'new' ); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <img data-magnify-src="old" src="https://i.stack.imgur.com/uImwY.jpg?s=48&g=1" id="bigImg"/> </div> <div> <div>click the image below and change the data attribute of the image above</div> <img class='img-miniatura' src="https://i.stack.imgur.com/uImwY.jpg?s=48&g=1"/> </div> 

You need to use .attr , not .prop

For example:

$(function(){
  $('.img-miniatura').click(function(e){
    $('#bigImg').attr('data-magnify-src', 'new');
  });
});    

See here: https://jsfiddle.net/1hbkv75q/2/

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