2010-11-28 52 views
0

我试图实现一个HTML文档中下面的代码:嵌入javascript到HTML文档中

$(function() { 

       $.ajax({ 
       type: "GET", 
       url: "/projects/img/Bathurst/PhotoGallery.xml", // location of your gallery's xml file 
       dataType: "xml", 
       success: function(xml) { 


        $(xml).find('img').each(function() { 

         var location = '/projects/img/Bathurst/'; // relative path to the directory that holds your images 
         var url = $(this).attr('src'); 
         var alt = $(this).attr('alt'); 

         $('<li></li>').html('<a href="'+location+''+url+'" rel="shadowbox[gallery]"><img class="thumb" src="'+location+''+url+'" alt="'+alt+'" title="'+alt+'" /></a>').appendTo('#gallery-ul'); 


        }); 
        $('<script type="text/javascript"></script>').html('Shadowbox.clearCache(); Shadowbox.setup();').appendTo('#photo-gallery'); 
       } 


      }); 
}); 

代码工作完美,当我在一个外部.js文件中使用它,但我不能让它工作当我实现它时,它只是在代码中出现错误。

我是否错过了某些东西和任何人有任何建议呢?为什么我需要实现它,以防万一有一个问题,我正在构建自定义Web应用程序,并且“/projects/img/Bathurst/PhotoGallery.xml”和“/ projects/img/Bathurst /”行是动态变量。

所有的答案非常感谢! :)

+0

你看到了什么错误? – Jan 2010-11-28 15:08:50

+2

请定义“它只是呈现”和“代码中的错误”。它究竟是如何失败?另外,你如何将它嵌入到文档中?请提供足够的封装HTML来重现问题。 – David 2010-11-28 15:09:54

+0

你应该学习Javascript的基础知识。 – SLaks 2010-11-28 22:40:07

回答

2

有问题的线路($('<script type="text/javascript">...)是运行JavaScript的两行convluted和不必要地复杂化的方式。

您应该用简单的方法调用来替换它。 (Shadowbox.clearCache(); Shadowbox.setup();

2

script中不能有</script>。 变化

$('<script type="text/javascript"></script>') 

$('<script type="text/javascript"><\/script>')