0

我正在使用 ajax 调用将 mycontent.php 加载到我的模式对话框中。我正在使用 AJAX,但是当 mycontent.php 位于模式窗口中时,这 3 个 javascript 标记没有被包含或执行。

我已经尝试过 eval 语句,但似乎不起作用。

这是我的代码:

main.html

 <a class="dialog" title="testvideo" href="mycontent.php">
 <img class="imageleft" width="200" height="150" src="assets/images/photos/deya/thumbnail.jpg"></a>

modal.js

    $(document).ready(function() {                

       $('a.dialog').click(function (event) {
          if ($("body").hasClass("res-full")){
             event.preventDefault();
             $this = $(this);

             var URL     = $(this).attr('href');
             var dialogbox = document.getElementById('dialog');
             var dialogOptions = {
                        height: 'auto',
                        width: 'auto',
                        modal: true,

              //         open: function(event, ui){

              //         },

                       close: function(event, ui){
                            $('#dialog').empty(); 
                       }
          };

       if(dialogbox==null) {
          $this.after("<div id=\"dialog\"></div>");
       }

       jQuery('#dialog').load(URL + " #content", function() {eval($('script').html())}).dialog(dialogOptions);


      }            
      });


    });

我的内容.php

   <div id="content">
       <div id="VidPlayerPlaceholder_7001_wrapper">
        <object id="VidPlayerPlaceholder_7001" width="100%" height="100%" type="application/x-shockwave-flash" data="video/player/player6.swf" bgcolor="#000000" name="VidPlayerPlaceholder_7001" tabindex="0">
        <div id="VidPlayerPlaceholder_7001_jwpsrv" style="position: absolute; top: 0px; z-index: 10;"></div>
   </div>
   <script type="text/javascript" src="http://www.mycompany.com/video/player/js/jwplayerv6.js"></script>
   <script type="text/javascript">jwplayer.key="jCz8k6TcT9i6M5vRXEI474+6dfNf9a7gHBbRfA==";</script>
   <script type="text/javascript">
          jwplayer('VidPlayerPlaceholder_4029').setup({ 
                    flashplayer: "http://www.company.com/video/player/player6.swf",
                    html5player: "http://www.company.com/video/player/js/jwplayer.html5.js",
                    playlist: [
            { image: "http://pciture.jpg", file: "rtmp://DEYA/video3.m4v"}
        ],
        width: '505',
        height: '430',
        stretch: 'uniform',
        autostart: 'false',
        repeat: 'false',

        logo: {
            file: 'http://ccopyright.png',
            link: 'http://www.mycompany.ca',
            hide: '',
            position: 'top-left'
        },
        rtmp: {
            bufferlength: '5'
        },
        primary: 'html5'
        });
        jwplayer('VidPlayerPlaceholder_4029').setVolume(50);
        if(0 > 0){
            jwplayer('VidPlayerPlaceholder_4029').stop();       
            jwplayer('VidPlayerPlaceholder_4029').seek(0);      
            if('false' == 'false'){
                jwplayer('VidPlayerPlaceholder_4029').pause();
            }
    }
</script> 

如果有人可以提供帮助,将不胜感激。

4

3 回答 3

1

As i mentioned in the comments you could try doing it this way, I just built on Rocket Hazmat's answer - but replaced the fragment div with the implementation jquery uses for its load() with the #fragmentdiv param.

Now what you'll also want to do is call your jwplayer inside the success function (done()), paste in the JS for your jwplayer setup (the inline JS) you have from your php file into the done() function as commented below - and see if that works.

(function($) { //wrap to local scope
    $(document).ready(function() {
        var fragment_div = '#content';
        var $target_div = '#dialog';

        $.ajax({
            url: url,
            dataType: 'html' //IE can be buggy on the jquery intelligent guess
        }).done(function(data) {
            var content = $('<div>').append($.parseHTML(data)).find(fragment_div);
            $('#dialog').html(content);

            //try
            $target_div.html($data.filter('script'));
            //or
            $target_div.html($('<script>').append($.parseHTML(data)).find('script');

           //try moving your jwplayer setup code here for testing, you can use bind() and trigger()
           //to create a custom event if you want to keep the js as you have it now in mycontent.php
           jwplayer('VidPlayerPlaceholder_4029').setup({ /*..etc */ });

           // Open dialog
           $('#dialog').dialog(dialogOptions);
        });
    });
})(jQuery);

EDIT is there any extra content in your .php? If not you could just use .load() without the #fragment param. That will load the javascript aswell.

于 2013-10-08T20:18:13.833 回答
1

你打电话loadURL + " #content"。在 URL 之后添加一个选择器使 jQuery 只使用页面的那一部分而忽略它的其余部分。因此,您的<script>标签永远不会加载到 DOM 中,也不会在回调中可用。

尝试使用$.get获取整个页面,然后将每个部分放在它所属的位置。

$.get(url, function(html){
    // Parse HTML response
    var $data = $(html);

    // Add content to DOM
    $('#dialog').html($data.filter('#content').html());

    // Run scripts
    $data.filter('script').each(function(){
        // http://stackoverflow.com/a/12201713/206403
        var scriptTag = document.createElement('script');
        scriptTag.text = $(this).html();
        document.body.appendChild(scriptTag);
    });

   // Open dialog
   $('#dialog').dialog(dialogOptions);

}, 'html');
于 2013-10-08T16:19:34.260 回答
0

将 mycontent.php 上的 javascript 放在一个函数中,然后您可以在 .load() 完成时从 main.html 调用该函数。在http://api.jquery.com/load/上阅读有关 .load() 的 onComplete 功能的更多信息

于 2013-10-08T16:24:52.073 回答