将图像插入模态对话框

我想在 Modal 中插入图像,但我需要使用变量的值,data-image因为它会根据图像的值而变化。


<script type="text/javascript">

        $('#myModal').on('show.bs.modal', function (event) {

            var button = $(event.relatedTarget) // Button that triggered the modal

            var title = button.data('title')

            var overview = button.data('overview')

            var image = button.data('image')

            var url = "<img src='https://image.tmdb.org/t/p/w600_and_h900_bestv2/+image+'></img>"

            var modal = $(this)

            modal.find('#title').text(title)

            modal.find('#image').attr('src',url)

            modal.find('#overview').text(overview)

        })

    </script>

                                <a href="#" role="button" data-id="{{$item['id']}}" data-title="{{$item['title']}}" data-image="{{$item['imagem']}}" data-overview="{{$item['overview']}}" data-toggle="modal" data-target="#myModal">


<div class="modal fade bd-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

    <div class="modal-dialog modal-lg">

        <div class="modal-content">

            <div class="modal-header">

                <h5 class="modal-title" id="exampleModalLongTitle"><div id="title"></div></h5>

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                    <span aria-hidden="true">&times;</span>

                </button>

            </div>

            <p><div id="image"></div></p>

            <p><div id="overview"></div></p>

        </div>

    </div>

</div>

我尝试使用一些形状,如文本、val、innerhtml,但没有用。让它按照我的需要工作的最好方法是什么?



元芳怎么了
浏览 179回答 2
2回答

跃然一笑

您需要使用img元素来反对 adiv来设置img在您的模式中动态显示您的内容和其他内容。它不起作用的原因是DOM内容已准备就绪,并且您正在将样式应用于 adiv并且它无法加载图像asynchronously,这就是为什么您看不到任何东西的原因。我已经添加了demo数据和其他几个按钮来表明它现在所有的工作都来自不同的data地方data-attributes.现场工作演示:$('#myModal').on('show.bs.modal', function(event) {&nbsp; var button = $(event.relatedTarget) // Button that triggered the modal&nbsp; var title = button.data('title')&nbsp; var overview = button.data('overview')&nbsp; var image = button.data('image')&nbsp; var modal = $(this)&nbsp; modal.find('#title').text(title)&nbsp; modal.find('#image').attr('src', image)&nbsp; modal.find('#overview').text(overview)})<!-- Latest compiled and minified CSS --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"><!-- jQuery library --><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script><!-- Popper JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script><!-- Latest compiled JavaScript --><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script><a href="#" role="button" data-id="Foo" data-title="Foo" data-image="https://via.placeholder.com/150" data-overview="Foo" data-toggle="modal" data-target="#myModal">Click Me</a><br><a href="#" role="button" data-id="Bar" data-title="Bar" data-image="https://via.placeholder.com/300" data-overview="Bar" data-toggle="modal" data-target="#myModal">Click Me 2</a><br><a href="#" role="button" data-id="Example" data-title="Example" data-image="https://via.placeholder.com/350" data-overview="Example" data-toggle="modal" data-target="#myModal">Click Me 3</a><div class="modal fade bd-example-modal-lg" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">&nbsp; <div class="modal-dialog modal-lg">&nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; <div class="modal-header">&nbsp; &nbsp; &nbsp; &nbsp; <h5 class="modal-title" id="exampleModalLongTitle">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="title"></div>&nbsp; &nbsp; &nbsp; &nbsp; </h5>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="modal" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="modal-body">&nbsp; &nbsp; &nbsp; &nbsp; <p><img src="" id="image" /></p>&nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div id="overview"></div>&nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div>

万千封印

首先,了解模态对话框是什么:它只是一个 div(带有内容),其位置使其位于页面其余部分的顶部,通常伴随着另一个 div,直接位于其下方并使其余部分部分变暗页面,同时防止用户点击下面的任何内容。这两个 div 开始隐藏,但可以根据用户活动显示,然后重新隐藏。这个modal词表明这个 div 结构有焦点——在模式对话框关闭之前,用户不能与页面的任何其他部分交互。这是通过高度/宽度为 100% 并定位(通过 z-index)以堆叠在对话框和页面其余部分之间的第二个(覆盖)div 来实现的。需要注意的重要一点是,它们只是页面上存在的普通 div 结构 - 或者随意添加到页面中。这意味着,您可以像更改任何其他 div 的内容一样轻松地更改模态 div 结构内的内容(文本或 html)。模态 div 结构没有什么不同。Bootstraps 模态对话框完全相同,但它们包含一些额外的嘶嘶声,增加了一些额外的视觉酷感,并使它们更容易使用。除此之外,它们就像自制的模态对话框——与普通的 div 结构完全一样。为了演示,我们将创建一个超级简单的自制模式对话框并使用它来执行您的请求。请注意以下有关自制模式对话框的信息:(a) 我们使用position:fixed或position:absolute从通常的 HTML 流程中删除模态 div 结构——允许它位于页面其余部分的顶部。(b) 我们使用z-index将模态 div 结构置于页面其余部分之上(c) 我们添加第二个 div(“叠加层”),它位于页面顶部,但位于模态下方。其目的是使模式对话框下方的页面变暗,并防止用户在完成对话框之前单击页面上的任何内容。$('button').click(function(){&nbsp; let img = $('#modal').data('image');&nbsp; $('#modal-content').html(`<img src="${img}" />`);&nbsp; $('#overlay, #modal').fadeIn();});$('#modal-close').click(function(){&nbsp; $('#overlay, #modal').fadeOut();});#overlay{z-index:998;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,.7);}#modal{z-index:999;position:fixed;top:15vh;left:25vw;width:50vw;height:30vh;}#modal-close{position:absolute;top:0;right:0;padding:10px;font-size:2em;border:1px solid grey;}#modal-close:hover{color:dodgerblue; border:1px solid dodgerblue;cursor:pointer;}#overlay, #modal{display:none;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id='overlay'></div><div id='modal' data-image='http://placekitten.com/400/300'>&nbsp; &nbsp;<div id='modal-close'>X</div>&nbsp; &nbsp;<div id='modal-content'></div></div><button>Show Modal</button>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript