【问题标题】:jQuery mobile link multiple html documentsjQuery移动链接多个html文档
【发布时间】:2012-05-31 04:30:46
【问题描述】:

我有多个 html 文档,它们共享一个 css 样式表(不是一个 html 中的多个页面 div 部分),用于使用 jQuery 移动框架的 Web 应用程序项目。 所有 html 文档单独工作都很好,但如果一个页面从另一个页面的链接打开,则所有脚本和样式都不再适用。有没有办法让每个 html 页面在链接时保持刷新?我在<a> 标签中尝试过data-ajax="false" 和rel="external",但它们没有用。您的帮助将不胜感激!

是的,每个html在head标签中都有插件链接:

<link rel="stylesheet" href="style/custom.css"> 
<link rel="stylesheet" href="style/jquery.mobile-1.0.1.min.css" /> 
<script src="js/jquery-1.6.4.min.js"></script> 
<script src="js/jquery.mobile-1.0.1.min.js"></script> 
<script type="text/javascript">
 $(document).ready(function(event){ 
//custom script in each html 
}); 

而 html 是通过其标签中的标签链接的:

<a href="page1.html">page 1</a> 
<a href="page2.html">page 2</a> 
<a href="page3.html">page 3</a>

链接有效,但当从前一页打开新 html 时,不再应用自定义样式表和脚本。如果我在标签中添加 data-ajax="false" 或 rel="external" ,即使链接也不起作用了。

感谢您查看代码!

PS。抱歉,我不熟悉 jsfiddle 并为两个单独的 html 文件复制了我的代码。如果我先打开 page2.html,滑块可以正常工作。但是如果我从 page1.html 中的菜单打开它,滑块就不起作用了。

这是page1.html:

<!doctype html> 
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(event){
    $(document).toggle(
      function(){
      $('.navigation').css("display", "block");},
      function(){
      $('.navigation').css("display", "none");
      });
});
</script>
<style type="text/css">
.navigation{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:40px;
    background:#cfcfcf;
    display:none;
}
.menu{
    float:left;
    margin: 10px 20px;
    text-decoration:none;
}
.main{
    margin: 50px;
}
</style>
<body>
<div data-role="page">
    <div id="page1">
        <div class="navigation">
            <a class="menu" href="page1.html">page 1</a> 
            <a class="menu" href="page2.html">page 2</a> 
        </div>
        <p class="main"> this is page1 <br> click anywhere</p>
    </div>
</div>
</body>
</html>

这是page2.html:

<!doctype html> 
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(event){
    $(document).toggle(
      function(){
      $('.navigation').css("display", "block");},
      function(){
      $('.navigation').css("display", "none");
      });

    $('.slider').change(function(){
        var wid= $(this).val() + "px";
        $('#box').css("width", wid);
    });  
});
</script>
<style type="text/css">
.navigation{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:40px;
    background:#cfcfcf;
    display:none;
}
.menu{
    float:left;
    margin: 10px 20px;
    text-decoration:none;
}
.main{
    margin: 50px;
}

#box{
    width:10px;
    height:30px;
    margin: 0px 50px;
    background:#000;
}

</style>
<body>
<div data-role="page">
    <div id="page2">
        <div class="navigation">
            <a class="menu" href="page1.html">page 1</a> 
            <a class="menu" href="page2.html">page 2</a> 
        </div>
        <p class="main"> this is page2 <br>adjust box width with slider </p>
        <div id="box"></div>
        <input type="range" class="slider" min="5" max="600" step="1" value="10"/> 
    </div>
</div>
</body>
</html>

【问题讨论】:

  • 您有任何代码/示例/演示供我们查看吗?在没有看到任何东西的情况下很难说出问题所在。
  • 使用jsfiddle.net 显示示例
  • 每个 html 文档是否有一个 head 包含您的 css 和 script 标签?
  • 我在上面添加了更多代码描述。感谢收看。

标签: jquery html ajax mobile


【解决方案1】:

把你的css和脚本放到每个页面的head标签上


试试这个

 <!doctype html> 
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />

<script type="text/javascript">



$( document ).delegate("#page1", "pageinit", function() {     

    $(document).toggle(
      function(){
      $('.navigation').css("display", "block");},
      function(){
      $('.navigation').css("display", "none");
      });                 
});

</script>
<style type="text/css">
.navigation{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:40px;
    background:#cfcfcf;
    display:none;
}
.menu{
    float:left;
    margin: 10px 20px;
    text-decoration:none;
}
.main{
    margin: 50px;
}
</style>
<body>
<div data-role="page" id="page1">
    <div>
        <div class="navigation">
            <a class="menu" href="page1.html">page 1</a> 
            <a class="menu" href="page2.html">page 2</a> 
        </div>
        <p class="main"> this is page1 <br> click anywhere</p>
    </div>
</div>

<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
</body>
</html>

还有 page2.html ,

 <!doctype html> 
<html>
<head>   

<body>


<div data-role="page" id="page2">

 <script type="text/javascript">


$( document ).delegate("#page2", "pageinit", function() { 
    $(document).toggle(
      function(){
      $('.navigation').css("display", "block");},
      function(){
      $('.navigation').css("display", "none");
      });

    $('.slider').change(function(){
        var wid= $(this).val() + "px";
        $('#box').css("width", wid);
    });  
});
</script>
<style type="text/css">
.navigation{
    position:fixed;
    top:0;
    left:0;
    width:100%;
    height:40px;
    background:#cfcfcf;
    display:none;
}
.menu{
    float:left;
    margin: 10px 20px;
    text-decoration:none;
}
.main{
    margin: 50px;
}

#box{
    width:10px;
    height:30px;
    margin: 0px 50px;
    background:#000;
}

</style>
    <div >
        <div class="navigation">
            <a class="menu" href="page1.html">page 1</a> 
            <a class="menu" href="page2.html">page 2</a> 
        </div>
        <p class="main"> this is page2 <br>adjust box width with slider </p>
        <div id="box"></div>
        <input type="range" class="slider" min="5" max="600" step="1" value="10"/> 
    </div>
</div>
</body>
</html>

试试这个

【讨论】:

  • 内部样式表使样式应用于不同的页面,但自定义脚本仍然不起作用。例如,我有调整图像宽度的脚本,这在链接的 html 中不起作用:$('#图片').css("宽度", 滑块值);
  • 我在我的问题中复制了上面两页的完整代码。谢谢!
  • 最先加载的页面是什么?
  • 啊,真的吗?您是否也先打开 page1.html,然后从其菜单中单击 page2?当我尝试时,它永远无法在不同的计算机和不同的浏览器中运行。
  • 是的。我首先打开 page1.html,然后单击 page2 链接。在第 2 页,滑块工作正常。你能告诉我你的应用程序中的第一页是什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2022-01-13
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多