简体   繁体   English

单击链接后加载模态

[英]Load modal after click link

I've been trying to load a modal content after clicking on link in this case "About Product". 在这种情况下,“关于产品”,单击链接后,我一直试图加载模式内容。 Atm on page is more article and every article got modal. 页面上的Atm是更多文章,并且每篇文章都有模态。 So modal's alose been load with page and modal's make web page slower because evrey single modal been load. 因此,由于evrey单个模式已加载,因此模态的页面已被加载,而模式的使网页变慢。

I want to make javascript when i click "About Product" the model will start to load or just: 当我单击“关于产品”时,我想制作JavaScript,模型将开始加载或仅:

<iframe width="100%" height="540px" src="'.$row['video_url'].'" frameborder="0" allowfullscreen></iframe>

Because this part make page slow 因为这部分会使页面变慢

There is rest of code: 还有其余的代码:

<div class="item-col col-xs-12 col-sm-4 col-md-3">
    <div class="product-wrapper">
        <div class="list-col4">
            <img src="../images/'.$row['productimage'].'" alt="'.$row['productname'].'">
        </div>
        <div class="list-col8" style="padding-bottom: 15px;">
            <div class="gridview">
                <div class="padding-games">
                    <span class="special-price"><span><center>'.$row['productname'].'</center></span></span>
                </div>
            </div>
            <a data-toggle="modal" data-target="#'.$row['id'].'">About Product</a>
            <a href="product-'.$row['title'].'">More</a>
        </div>
        <div class="clearfix"></div>
    </div>
</div>
<!--   product end -->

<!-- Modal -->
<div id="'.$row['id'].'" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <center><h4 class="modal-title"><img src="../images/'.$row['productimage'].'" alt="'.$row['productname'].'"></h4></center>
            </div>
            <div class="modal-body">
                <iframe width="100%" height="540px" src="'.$row['video_url'].'" frameborder="0" allowfullscreen></iframe>
            </div>
        </div>
    </div>
</div>

OK we can bind the a click event and use it to load the iframe src property, if you don't want to load straight away.. 好的,如果您不想立即加载,我们可以绑定a click事件并使用它来加载iframe src属性。

html : We add a data attribute data-link and add iframe link here to use later. html :我们添加了一个数据属性data-link并在此处添加了iframe链接以供以后使用。 We also add class to identify but you can use id if you prefer. 我们还添加了class来标识,但是如果您愿意,可以使用id

<a class="about_product" data-toggle="modal" data-link="'.$row['video_url'].'"  data-target="#'.$row['id'].'">About Product</a>

iframe IFRAME

<iframe id="myFrame" width="100%" height="540px" frameborder="0" allowfullscreen></iframe>

script : Here we attach click event to the link with class .about_product . script :这里,我们将click事件附加到类.about_product的链接上。 onclick data-src reference is used and set to the iframe 使用了onclick data-src参考并将其设置为iframe

$(document).ready(function () {
    $(".about_product").click(function (e) {
        e.preventDefault();
        $("#myFrame").attr('src', this.dataset.link);       
    })
});

This however is not tested in conjunction with bootstrap js files, so let us know how it goes. 但是,这并未与bootstrap js文件一起进行测试,因此请告诉我们它的运行方式。

Update: I created a jsfiddle to show how this works. 更新:我创建了一个jsfiddle来展示它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM