jQuery dialog with dynamic content

青春壹個敷衍的年華 提交于 2019-12-20 10:25:09

问题


I want to create a jQuery dialog on-the-fly. I'm using this:

var newDiv = $(document.createElement('div')); 
$(newDiv).html('hello there');
$(newDiv).dialog();

I then have this in the html header:

<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.1.custom.min.js"></script>

When I try running the JS in IE7 I get the following error on the $(newDiv).dialog(); line : Object doesn't support this property or method.

Anyone know what's going on?


回答1:


Your code works, you can test it here, that means you probably have a script include problem, make sure that your files are under a js folder beside the page, or if you intended them to be from site root, use /js instead.

Or, consider using a CDN.

You can make your code a bit more efficient (I realize it's just a test), like this:

var newDiv = $(document.createElement('div')); 
newDiv.html('hello there');
newDiv.dialog();

This works because newDiv is already a jQuery element, no reason to clone the object each time...or a bit shorter:

$('<div />').html('hello there').dialog();



回答2:


Here is an alternative way of creating dialogs and their messages dynamically:

     $('<div></div>').dialog({
        modal: true,
        title: "Confirmation",
        open: function() {
          var markup = 'Hello World';
          $(this).html(markup);
        },
        buttons: {
          Ok: function() {
            $( this ).dialog( "close" );
          }
        }
      });  //end confirm dialog

See it in action: http://jsfiddle.net/DYbwb/




回答3:


The code is good, what you need is a reference of jquery and jquery ui

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


来源:https://stackoverflow.com/questions/3423842/jquery-dialog-with-dynamic-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!