简体   繁体   中英

How to dynamically insert images into Bootstrap Popover

I want to use popover to show product images. Any idea how I can grab the image from the data-img attribute correctly? Thank you.

<a href='#' class='toggle-popover' title='Hello World' data-img='http://www.google.com/intl/en_ALL/images/srpr/logo11w.png' data-trigger='focus' data-toggle='popover'>Product Name</a>
$(document).ready(function() {
  $('[data-toggle="popover"]').popover({
    title: '',
    trigger: 'hover',
    content: '<div class="row">' +
      '<div class="col-sm-12 text-center ">' +
      '<div class="content3">' +
      '<div class="pt-3"><img src="' + $(this).data('img') + '"/></div>' +
      '</div>' +
      '</div>' +
      '</div>',
    html: true
  });
});

$(this).data('img') shows as undefined .

You can use .each() to iterate and set the popover content using current context ie this

$('[data-toggle="popover"]').each(function(){
    $(this).popover({
    title: '',
    trigger: 'hover',
    content: '<div class="row">' +
      '<div class="col-sm-12 text-center ">' +
      '<div class="content3">' +
      '<div class="pt-3"><img src="' + $(this).data('img') + '"/></div>' +
      '</div>' +
      '</div>' +
      '</div>',
    html: true
  })
});

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