在js文件中使用jQuery [英] using jQuery in a js file

查看:107
本文介绍了在js文件中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过include < code>< script type =text / javascriptsrc =scripts / jquery-1.10.2.js>< / script> 在HTML文件中,我可以直接在这个文件中写入jQuery。但是,我没有在单个文件上工作,因此,html中太多的js代码看起来不太好。 js文件中可以使用jQuery吗?所以我可以在一个独立的js文件中加入一些常用的功能。

创建一个名为 js 的文件夹。例如 main.js



您的js文件应该像下面这样开始: main.js strong>

  $(document).ready(function(){
// code here
} );

请注意,你可以使用 $(document).load(function()



在你的 index.html 中将这个放在页脚中: index.html

 <!DOCTYPE HTML> 

< html>

< head> ;
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< title>您的网站< / title>
< / head>

< body>
<! - ============ MAIN-CONTAINER ======== ==== - >
< section>

< / section>
<! - ============ SIDEBAR =================== - >
< aside>
< / aside>
<! - ============ FOOTER ==================== - >
< footer>
<< b< ; script src =http://code.jquery.com/jquery-2.0.0.min.js>< / script>
<脚本SRC = JS / main.js >< /脚本>
< / footer>

< / body>

< / html>

获得最佳表现。



<如果你想使用它作为

 < script src =scripts / strong> alfernative 的jquery-2.0.0.min.js>< /脚本> 

下载:



http://code.jquery.com/jquery-2.0.0.min.js



并将其放入名为脚本的新文件夹中并替换



< script src =http://code.jquery.com/jquery-2.0.0.min.js>< / script>

$ b



 < script src =脚本/ jQuery的2.0.0.min.js>< /脚本> 

在您的页脚中是这样的:

 < footer> 
< script src =scripts / jquery-2.0.0.min.js>< / script>
< script src =js / main.js>< / script>
< / footer>


I know by include <script type="text/javascript" src="scripts/jquery-1.10.2.js"></script> in HTML file, I can write in jQuery in this file directly. However, I am not working on a single file, thus, too much js code in the html doesn't look good. Can there be some method to use jQuery in js file? So I can put some common function in a standalone js file.

解决方案

create a folder with the name js and inside put your js file for example main.js

your js file should start like : main.js

$(document).ready(function(){
    //code here
});

note that you can use $(document).load(function() too

In your index.html put this in the footer like this : index.html

    <!DOCTYPE HTML>

    <html>

      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Your Website</title>
      </head>

      <body>
        <!--============MAIN-CONTAINER============ -->
        <section>

        </section>
        <!--============SIDEBAR=================== -->
        <aside>
        </aside>
        <!--============FOOTER==================== -->
        <footer>
            <script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>
            <script src="js/main.js"></script>
        </footer>

      </body>

    </html>

for the best performance.

alfernative if you want to use it as

<script src="scripts/jquery-2.0.0.min.js"></script> 

download this:

http://code.jquery.com/jquery-2.0.0.min.js

and put it into a new folder with the name scripts and replace

<script src="http://code.jquery.com/jquery-2.0.0.min.js"></script>

with

<script src="scripts/jquery-2.0.0.min.js"></script>

in your footer like this:

    <footer>
        <script src="scripts/jquery-2.0.0.min.js"></script>
        <script src="js/main.js"></script>
    </footer>

这篇关于在js文件中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆