无法从链接的 html 文件调用 .Js 文件 - JQuery mobile
我正在使用 Jquery 移动框架。我有 2 个 html 文件,每个文件都有两个按钮。单击一个按钮将导航到其他 html 文件。每个 html 文件中加载的 Javascript 将处理 HTML 文件中的单击事件。这里的问题是,无法调用从第一个 html 文件导航的第二个 html 文件加载的 .JS。它显示错误。
错误:p.attr("href") 未定义
源文件:file:///Users/Aspire/Development/JQuery%20Mobile/sample/jquery.mobile-1.0a2.min.js
线路:96
出了什么问题?
编辑:test.html
html 标签。 Headertag 下
<link rel="stylesheet" href="jquery.mobile-1.0a2.min.css" />
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.mobile-1.0a2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#test').click(function(){
alert('This is a general button!');
});
});
</script>
<Bodytag>
<div data-role="content">
<div data-role="page">
<a id="test" data-role="button" data-inline="true" data-theme="b">Click here</a>
<a href="~Development/JQuery Mobile/sample/test2.html" data-role="button" data-inline="true" >Navigate to two</a>
</div>
</div>
</Bodytag>
在第二个 html 的 。 test2.html
html 标签。在 Headertag 下,
<link rel="stylesheet" href="jquery.mobile-1.0a2.min.css" />
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.mobile-1.0a2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#test2').click(function(){
alert('This is a general button!');
});
});
</script>
<Bodytag>
<div data-role="content">
<div data-role="page">
<a id="test2" data-role="button" data-inline="true" data-theme="b">Click here</a>
<a href="~Development/JQuery Mobile/sample/test.html" data-role="button" data-inline="true" >Navigate to two</a>
</div>
</div>
</Bodytag>
在浏览器中单独打开 test2.html 时工作正常。即使返回 test.html 后,它也会给出相同的错误。未调用 JavaScript。即只有一次,当 .html 第一次启动时,Javascript 工作正常。导航到其他页面后,不会调用 Javascript。
I'm using Jquery mobile framework. I have 2 html files which has two buttons each. The clicking on one button will navigate to other html file. Javascript loaded in each html file will handle a click event in the HTML files. Problem here is , .JS loaded from second html file which is navigated from first html file could not be called. It shows error.
Error: p.attr("href") is undefined
Source File: file:///Users/Aspire/Development/JQuery%20Mobile/sample/jquery.mobile-1.0a2.min.js
Line: 96
What's going wrong?
Edited: test.html
html-tags. Under Headertag
<link rel="stylesheet" href="jquery.mobile-1.0a2.min.css" />
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.mobile-1.0a2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#test').click(function(){
alert('This is a general button!');
});
});
</script>
<Bodytag>
<div data-role="content">
<div data-role="page">
<a id="test" data-role="button" data-inline="true" data-theme="b">Click here</a>
<a href="~Development/JQuery Mobile/sample/test2.html" data-role="button" data-inline="true" >Navigate to two</a>
</div>
</div>
</Bodytag>
In the second html. test2.html
html-tags. Under Headertag
<link rel="stylesheet" href="jquery.mobile-1.0a2.min.css" />
<script src="jquery-1.4.4.min.js"></script>
<script src="jquery.mobile-1.0a2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#test2').click(function(){
alert('This is a general button!');
});
});
</script>
<Bodytag>
<div data-role="content">
<div data-role="page">
<a id="test2" data-role="button" data-inline="true" data-theme="b">Click here</a>
<a href="~Development/JQuery Mobile/sample/test.html" data-role="button" data-inline="true" >Navigate to two</a>
</div>
</div>
</Bodytag>
It works fine when test2.html is opened separately in browser. Even after returning to test.html, it gives same error. Javascript is not called. i.e. only one time, Javascript works fine when .html is launched for first time. After navigation to other page, Javascript is not called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,第二个 html 中的 javascript 无法运行。我通过添加 rel="external" 来强制卸载来解决这个问题。
即
希望这也适合你。
I ran into the same problem, the javascript in second html just doesn't run. I worked-around the problem by adding rel="external" to force unload.
i.e.
Hope this works for you too.
克莱门特的回答很有帮助,因为没有 ajax 调用替换您的链接。
正常链接(不带 rel=exteral)是通过 ajax 调用获取的,并且仅获取正文并将其添加到当前页面的 DOM 中。
这就是为什么你的js不会被调用的原因。
所有js必须链接到外部文件中的所有页面。
Clement's answer helped, because there was no ajax call replacing your link.
A normal link (without rel=exteral) is being fetched with ajax call and only the body is taken and added to DOM of the current page.
That's why your js willnot be called.
All js must be linked to all pages in external files.