简体   繁体   English

如何在弹出模式中仅显示来自外部html文件的一个div ID?

[英]How can I show only one div id from an external html file inside a popup modal?

I have some content on an external html file that I am displaying in a popup modal using jQuery Modal . 我在使用jQuery Modal的弹出模式中显示的外部html文件上有一些内容。 I am able to display the whole page fine, but I would like to only show one div at a time depending on which link is clicked. 我可以很好地显示整个页面,但是我只想一次显示一个div,具体取决于单击哪个链接。

The content on the external page looks like this: 外部页面上的内容如下所示:

<div class="review" id="one">This is review #1</div>
<div class="review" id="two">This is review #2</div>
<div class="review" id="three">This is review #3</div>

The links on the main page are coded like: 主页上的链接编码如下:

<a class="link" href="external.html" data-target="#two" rel="modal:open">Read review</a>

Then I have some Javascript that I thought would allow it to only show the selected div id depending on which link is clicked: 然后我有一些Javascript,我认为该Javascript仅会根据单击的链接显示所选的div ID:

$(document).ready(function() {
  $('.link').on('click', function() {
    var target = $(this).data('target');

    $('.review').not(target).hide();
    $(target).show();
  });
});

This shows all the content from the external html and not just one selected div. 这显示了来自外部html的所有内容,而不仅仅是一个选定的div。

Is there a way to show only one div id from an external html loaded into a modal? 有没有一种方法可以显示从外部HTML加载到模式中的一个div ID?

Update: I have attempted to implement Bhojendra's solution below, but am now getting the error: 更新:我试图在下面实现Bhojendra的解决方案,但是现在出现错误:

"TypeError: null is not an object (evaluating 'this.$blocker.fadeOut')"

The link to view the code: https://drive.google.com/open?id=1n61Buty8oexE369jdxCi4lZ9M9wj2SaP 查看代码的链接: https : //drive.google.com/open?id=1n61Buty8oexE369jdxCi4lZ9M9wj2SaP

From the documentation example that you linked, you can do like: 从您链接的文档示例中,您可以执行以下操作:

$('.link').click(function(event) {
  event.preventDefault();
  target = $(this).data('target');
  $.get(this.href, function(html) {
    $(html).find(target).appendTo('body').modal();
  });
});

A better to have modal content wrapper in your body: 体内最好有模式内容包装器:

<div id="modal-content"></div>

Now, in jQuery, use this: 现在,在jQuery中,使用以下命令:

$('#modal-content').html($(content).find(target)).modal()
// html parameter renamed ^^ to content

I meant to use: 我打算使用:

$('.link').click(function(event) {
  event.preventDefault();
  target = $(this).data('target');
  $.get(this.href, function(content) {
    $('#modal-content').html($(content).find(target)).modal()
  });
});

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

相关问题 如何仅显示来自不同页面的ID的HTML div或td元素? - How can I show only HTML div or td elements ID'ed from a different page? 如何在这个模态 div 中显示这个 .php 文件? - How to show this .php file inside this modal div? HTML:如何让弹出窗口只显示在某个操作系统上? - HTML: How can I have a popup only show on a certain os? 如何在这个带有id的模态中显示.php文件? - How to show .php file inside this modal with id? 在div中使用GET参数显示来自外部php文件的内容 - Show content from external php file with GET parameter inside div 我如何才能使外部文件中的javascript仅在PHP文件中的div中的图像上起作用? - How could I make it such that javascript in an external file works only on an image in a div inside a PHP file? 我想把jquery $ .ajax()方法的响应放到bootstrap popup Modal里面的目标<div> - I want to put response from jquery $.ajax() method to a target <div> inside bootstrap popup Modal 如何仅从外部URL自动打开弹出框模式 - How to automatically open a popup-box modal only from an external URL 如何从外部 Javascript 文件修改 HTML 元素? - How can I modify an HTML element from an external Javascript File? 如何将div替换为另一个div(但外部html页面)? - How Can I replace a div with another div(but of external html page)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM