繁体   English   中英

在HTML文档中嵌入Javascript

[英]Embedding Javascript in html document

我正在尝试在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文件中使用该代码时,它可以完美地工作,但是当我实现它时,我无法使其正常工作,它只是在代码中出现错误。

我会丢失什么吗,有人对此有建议吗? 如果有人想知道的话,我之所以需要实现它,是因为我正在构建一个自定义的Webapp,而“ /projects/img/Bathurst/PhotoGallery.xml”行和“ / projects / img / Bathurst /”行是动态变量。

非常感谢所有答案! :)

你不能有一个</script>一个内部script 更改

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

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

有问题的行( $('<script type="text/javascript">... )是运行两行Javascript的既复杂又不必要的复杂方式。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM