How to load image into bootstrap modal with javascript

后端 未结 1 664
天涯浪人
天涯浪人 2021-02-19 19:28

My setup is 4 links (below). I want each link to open myModal, but depending on which link gets clicked, a different image file should load in the modal. I got it working for

1条回答
  •  有刺的猬
    2021-02-19 20:09

    By using jquery .click you can easily achieve this (with html5 data attribute too).

    
    

    Script:

    $('li a').click(function (e) {
        $('#myModal img').attr('src', $(this).attr('data-img-url'));
    });
    

    Fiddle: http://jsfiddle.net/vLSWH/292/

    Note: Though you did not specify using jQuery, twitter-bootstrap modal require you to use jQuery anyway, so might as well use it.

    0 讨论(0)
提交回复
热议问题