从 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>
相关文章:
- 删除表,但不删除表单和文件 jQuery
- Ruby on Rails:“找不到文件'jquery.ui.datepicker'”
- 从 html 页面调用文件 jQuery 文件
- Javascript 文件修改了未从中调用它的 html 文件?Jquery 选择器
- 未呈现Javascript文件/Jquery
- 上传文件jQuery ajax MVC
- 我在哪里可以得到这个文件jquery.min.js
- 使用委托点击按钮下载文件JQuery
- 找不到文件“jquery-ui”
- JavaScript文件(jQuery步骤)未在Google Chrome中加载
- 自动内联CSS从CSS文件(jquery?ajax吗?php ?)
- 如何使用本地json文件jquery或angularjs实现文本框的自动完成
- 在JSPX文件中包含JS文件(JQuery)
- 如何将变量值从后面的代码传递到外部JavaScript文件(jQuery文件)
- 如何获得扩展图像文件jquery.filer
- 检测设备和交换CSS文件- jQuery
- 警报文本从文件JQuery / javascript
- 如何排序日期和字符串在同一列在jquery插件有文件jquery. datatable .js
- 移动谷歌地图api代码到单独的文件+ jquery
- 获取按钮id从js文件和使用在另一个js文件- jquery