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

javascript - How to dynamically load content from an external url, inside of a jquery ui modal dialog widget? - Stack Overflow

programmeradmin2浏览0评论

I asked this question before, but I don't think I explained properly for what I am trying to accomplish.

There are multiple links on my website and I want to open the content from the link in a jquery ui modal dialog widget.

I'm trying to use 'this' to reference the link that the user selects dynamically. What am I doing wrong here?

The code I'm using is below:

<a href="index.cs.asp?Process=comments&id=1" id="test">comment #1</a>
<a href="index.cs.asp?Process=comments&id=2" id="test">comment #2</a>
<a href="index.cs.asp?Process=comments&id=3" id="test">comment #3</a>
<div id="somediv"></div>                                    
    <script type="text/javascript">

    $(document).ready(function() {  

        $("#somediv").load(this.getTrigger().attr("href")).dialog({
            autoOpen: false,
            width: 400,
            modal: true
        });     
        $("#test").click(function(){$( "#somediv" ).dialog( "open" );});
});

</script>

I asked this question before, but I don't think I explained properly for what I am trying to accomplish.

There are multiple links on my website and I want to open the content from the link in a jquery ui modal dialog widget.

I'm trying to use 'this' to reference the link that the user selects dynamically. What am I doing wrong here?

The code I'm using is below:

<a href="index.cs.asp?Process=comments&id=1" id="test">comment #1</a>
<a href="index.cs.asp?Process=comments&id=2" id="test">comment #2</a>
<a href="index.cs.asp?Process=comments&id=3" id="test">comment #3</a>
<div id="somediv"></div>                                    
    <script type="text/javascript">

    $(document).ready(function() {  

        $("#somediv").load(this.getTrigger().attr("href")).dialog({
            autoOpen: false,
            width: 400,
            modal: true
        });     
        $("#test").click(function(){$( "#somediv" ).dialog( "open" );});
});

</script>
Share Improve this question edited Nov 19, 2013 at 16:34 James-Jesse Drinkard 15.7k16 gold badges118 silver badges140 bronze badges asked Mar 23, 2013 at 0:11 EfeEfe 9543 gold badges20 silver badges37 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 16

http://jsfiddle.net/qp7NP/

A couple changes: changed ID to Class and used IFrame.

<a href="http://wikipedia.com/" class="test">comment #1</a><br>
<a href="http://ebay.com/" class="test">comment #2</a><br>
<a href="http://ask.com/" class="test" >comment #3</a><br>
<div id="somediv" title="this is a dialog" style="display:none;">
    <iframe id="thedialog" width="350" height="350"></iframe>
</div>
<script>
$(document).ready(function () {
    $(".test").click(function () {
        $("#thedialog").attr('src', $(this).attr("href"));
        $("#somediv").dialog({
            width: 400,
            height: 450,
            modal: true,
            close: function () {
                $("#thedialog").attr('src', "about:blank");
            }
        });
        return false;
    });
});
</script>

In case you want to pull in the HTML instead of IFrame, you will have to use Ajax (XMLHttpRequest), something like this: http://jsfiddle.net/qp7NP/1/

You can't have multiple elements with the same Id. Change your links to to class="test" instead and therefore your click event to $('.test').click().

Also if you still have problems, and I remember I had some similar issues because how JQUery Dialog behaves itself with the DOM. It will literally rip your #somediv out of content and append it to the bottom of a page to display that dialog. I solved my dynamic dialog loading issues with wrapping it in another div.

<div id="somediv-wrap">
  <div id="somediv">

  </div>
</div>

<script>
    $(document).ready(function() {  

        $("#somediv-wrap").dialog({
                  autoOpen: false,
                   width: 400, 
                   height:200,
                   modal: true
                  });  

        $(".test").click(function(event)
          {
              event.preventDefault();
              var link = $(this).attr('href');

              $("#somediv").load(link,function(){
               $( "#somediv-wrap" ).dialog( "open" );   
              });                        
      });
   });
</script>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论