如何在拥有 Ajax 和 CSS 时包含 Java 脚本

how to include java script when you have ajax and css?

本文关键字:包含 Java 脚本 CSS 拥有 Ajax      更新时间:2023-09-26

当我在索引中运行javascript时.html它工作正常,但我想将其分离为js文件所以我创建了 aram.js然后我向其中添加了我的代码我还在索引中包含了这一行:

<script type="text/javascript" src="aram.js"></script>

阿拉姆.js:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
            $(function() {
                $('#accordion > li').hover(
                    function () {
                        var $this = $(this);
                        $this.stop().animate({'width':'480px'},500);
                        $('.heading',$this).stop(true,true).fadeOut();
                        $('.bgDescription',$this).stop(true,true).slideDown(500);
                        $('.description',$this).stop(true,true).fadeIn();
                    },
                    function () {
                        var $this = $(this);
                        $this.stop().animate({'width':'115px'},1000);
                        $('.heading',$this).stop(true,true).fadeIn();
                        $('.description',$this).stop(true,true).fadeOut(500);
                        $('.bgDescription',$this).stop(true,true).slideUp(700);
                    }
                );
            });

我认为问题是它无法在索引中包含的 css 文件夹中找到我有样式.css的 css.html如下所示:

  <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"/>

你需要有脚本标签来在你的javascript包含文件上面包含jquery。这两个包含应该在你的 html 中。

喜欢这个:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="aram.js"></script>

然后这将首先加载jquery,以便可以执行您的javascript(使用jquery)。

你不能

在.js文件中写标签。 你也应该在 Index 中调用 jQuery.html。

如果 js 在 .js 文件中,则无需将 js 包装在 <script> 标签中。

此外,您不能在javascript文件中使用html标签。此脚本标记属于您的 html 文件,排序如下:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="aram.js"></script>

JS文件只有js

传统上,你会从HTML文件中引用jQuery javascript文件和你自己的javascript文件。 将 HTML 脚本标签放入 javascript 文件中将无法正常工作。

如果你真的只想在 HTML 文件中包含一个脚本元素,应该可以使用以下代码从该文件注入 jquery 代码:

document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>');

但是,在这种情况下,这种技术没有太多值得推荐的地方。