簡體   English   中英

從html頁面調用文件jQuery文件

[英]calling a file jQuery file from html page

我創建了一些使用Ajax刷新/更新兩個字段的測試代碼。 它工作得很好,現在,為了保持整潔,我創建了一個名為Ajax.js的文件

在我的所有字段的HTML頁面中,我想調用此文件並使其更新兩個變量。

我做了以下事情:

在我的HTML頁面中:

  <--- included the meta lines needed for the Ajax file ---->
  <--- included: my jQuery includes needed ---------->
  <---- my link to the Ajax.js file -------------->
 <script src="Ajax.js"></script>

 <----- made the call this way, I am not sure is right ------->
 <!----- Ajax Update Fields Function ----->
 <script>
   jQuery('nav').Ajax();
 </script>

該調用不起作用,它不會更新字段。 這是實際的Ajax.js代碼:

// JavaScript Document
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> 


$(function()
{
  setInterval(function()
   {
$.get("ajax_v00.html",function(data,textStatus, jqXHR)
 {
  var temperature=$(data).filter('#variable1').text()
  var time=$(data).filter('#variable2').text()
  $('#variable1').text(temperature)
  $('#variable2').text(time)
 })
 .fail(function(jqxhr,textStatus,errorThrown) //Callback failed
  {
   $('#errors').text("Errors:" + textStatus + " " + errorThrown)
  })
 .done(function() //Callback succeeded
  {
   $('#errors').text('Errors:');
  })
   },1000);
})

我怎么稱呼這個? 希望我能正確解釋自己?

把它放在你的html頁面的頭部......

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="Ajax.js"></script>

Ajax.js刪除腳本標記。

刪除jQuery('nav').Ajax(); 你不需要這個。 它正在尋找一個名為Ajax()的jQuery函數,它不存在。 您在Ajax.js代碼已經在頁面加載時運行,因為您已將其包裝在document.ready的速記版本中...

$(function() {
    setInterval(function() {
        $.get("ajax_v00.html",function(data,textStatus, jqXHR) {
            var temperature=$(data).filter('#variable1').text();
            var time=$(data).filter('#variable2').text();
            $('#variable1').text(temperature);
            $('#variable2').text(time);
        })
        .fail(function(jqxhr,textStatus,errorThrown) {
            $('#errors').text("Errors:" + textStatus + " " + errorThrown);
        })
        .done(function() {
            $('#errors').text('Errors:');
        })
    },1000);
});

請使用此腳本。

   <script>
    $(document).ready(function() {
                      jQuery('nav').Ajax();});
  </script>

暫無
暫無

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

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