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

javascript - twitter bootstrap modal not displaying remote image - Stack Overflow

programmeradmin7浏览0评论

I'm using twitter bootstrap's modal window to load a remote image

<a href="assets/500x300.gif" data-target="#image-preview" data-toggle="modal"><img alt="250x150" src="/assets/250x150.gif"></a>

I'm following these docs

The image is not rendering correctly in the modal.

I get a load of jumbled data instead -

What's happening? How can I fix?

I'm using twitter bootstrap's modal window to load a remote image

<a href="assets/500x300.gif" data-target="#image-preview" data-toggle="modal"><img alt="250x150" src="/assets/250x150.gif"></a>

I'm following these docs

The image is not rendering correctly in the modal.

I get a load of jumbled data instead -

What's happening? How can I fix?

Share Improve this question asked Oct 2, 2012 at 16:29 FinnnnFinnnn 3,5906 gold badges49 silver badges69 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Yes, I had this too. The answer I found is here.

I created a link like this:

<a href="gallery/blue.jpg" class="thumbnail img_modal">

//handler for link
$('.img_modal').on('click', function(e) {
    e.preventDefault();
    $("#modal_img_target").attr("src", this);
    $('#modal').modal('show');
});

//and modal code here

<div id="modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Title to go here</h3>
    </div>
    <div class="modal-body">
        <img id="modal_img_target">
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    </div>
</div>

When using href to specify remote content for a modal, Bootstrap uses jQuery's load method to fetch the content, and then sticks the retrieved content into the modal's body element. In short, it only makes sense when the remote content (the href value) loads HTML or text.

What you're seeing is expected, as it's the same thing that would happen if you opened the gif file in a text editor, and just pasted it into an HTML tag.

To have it work the way you want, the href needs to point to an HTML document that contains an <img> tag that references the image you want in the modal.

In post of Mark Robson – change 'this' to 'this.href':

$("#modal_img_target").attr("src", this);

=>

$("#modal_img_target").attr("src", this.href);

(I don't have reputation for ment)

发布评论

评论列表(0)

  1. 暂无评论