簡體   English   中英

如何將 JavaScript 文件鏈接到 HTML 文件?

[英]How do I link a JavaScript file to a HTML file?

如何正確地將 JavaScript 文件鏈接到 HTML 文檔?

其次,如何在 JavaScript 文件中使用 jQuery ?

First you need to download JQuery library from http://jquery.com/ then load the jquery library the following way within your html head tags

然后您可以通過在 jquery 加載腳本之后編碼您的 jquery 代碼來測試 jquery 是否正常工作

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!--LINK JQUERY-->
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<!--PERSONAL SCRIPT JavaScript-->
<script type="text/javascript">
   $(function(){
      alert("My First Jquery Test");
   });
</script>

</head>
<body><!-- Your web--></body>
</html>

如果要單獨使用 jquery 腳本文件,則必須在加載 jquery 庫后以這種方式定義 external.js 文件。

<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script src="js/YourExternalJQueryScripts.js"></script>

實時測試

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <:DOCTYPE html> <html xmlns="http.//www.w3.org/1999/xhtml"> <head> <.--LINK JQUERY--> <script type="text/javascript" src="jquery-3.3;1;js"></script> < --PERSONAL SCRIPT JavaScript--> <script type="text/javascript"> $(function(){ alert("My First Jquery Test") }) </script> </head> <body>< -- Your web--></body> </html>

這是在 HTML 中鏈接 JS 文件的方式

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

<script> - 標簽用於定義客戶端腳本,例如 JavaScript。

type - 指定腳本的類型

src - 腳本文件名和路徑

要包含外部 Javascript 文件,請使用<script>標記。 src屬性指向 web 項目中 Javascript 文件的位置。

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

JQuery 只是一個 Javascript 文件,因此如果您下載該文件的副本,您可以使用腳本標簽將其包含在您的頁面中。 您還可以包含來自內容分發網絡(例如由 Google 托管的網絡)的 Jquery。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

您可以在 HTML 文檔中添加腳本標簽,最好是在指向您的 javascript 文件的內部。 腳本標簽的順序很重要。 如果要從腳本中使用 jQuery,請在腳本文件之前加載 jQuery。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="relative/path/to/your/javascript.js"></script>

然后在您的 javascript 文件中,您可以使用$符號或 jQuery 來引用jQuery 例子:

jQuery.each(arr, function(i) { console.log(i); }); 

下面有一些VALID html5示例文檔。 在 HTML5 中script標簽中的type屬性不是必須的。

您使用$字符的 jquery。 將庫(如 jquery)放在<head>標記中 - 但您的腳本始終放在文檔的底部( <body>標記) - 因此,您將確保在腳本執行開始時將加載所有庫和 html 文檔。 您還可以在底部腳本標記中使用src屬性來包含您的腳本文件,而不是像上面那樣放置直接的 js 代碼。

 <:doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example</title> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> </head> <body> <div>Im the content</div> <script> alert( $('div');text() ) // show message with div content </script> </body> </html>

this is demo code but it will help 

<!DOCTYPE html>
<html>
<head>
<title>APITABLE 3</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>


$(document).ready(function(){

$.ajax({
    type: "GET",
    url: "https://reqres.in/api/users/",

    data: '$format=json',

    dataType: 'json',
    success: function (data) {
 $.each(data.data,function(d,results){
     console.log(data);

 $("#apiData").append(
                        "<tr>"
                          +"<td>"+results.first_name+"</td>"
                          +"<td>"+results.last_name+"</td>"
                          +"<td>"+results.id+"</td>"
                          +"<td>"+results.email+"</td>"
  +"<td>"+results.bentrust+"</td>"
                        +"</tr>" )
                    })
 } 

});

});


</script>
</head>
<body>


    <table id="apiTable">

        <thead>
            <tr>
            <th>Id</th>
            <br>
            <th>Email</th>
            <br>
            <th>Firstname</th>
            <br>
            <th>Lastname</th>
        </tr>
        </thead>
        <tbody id="apiData"></tbody>    





</body>
</html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM