2010-11-28 77 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>')