无法从链接的 html 文件调用 .Js 文件 - JQuery mobile

发布于 2024-10-07 04:04:59 字数 2226 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话里做英雄 2024-10-14 04:04:59

我遇到了同样的问题,第二个 html 中的 javascript 无法运行。我通过添加 rel="external" 来强制卸载来解决这个问题。

<a href="test2.html?" id="test2" data-role="button" data-inline="true" data-theme="b" rel="external">Click here</a>

希望这也适合你。

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.

<a href="test2.html?" id="test2" data-role="button" data-inline="true" data-theme="b" rel="external">Click here</a>

Hope this works for you too.

无所的.畏惧 2024-10-14 04:04:59

克莱门特的回答很有帮助,因为没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文