【问题标题】:How to link to internal pages using changePage?如何使用 changePage 链接到内部页面?
【发布时间】:2017-03-30 09:40:55
【问题描述】:

我目前有一个 index.html 文件,其中包含 3 页(具有单独的 ID)。我还有 3 个按钮显示在所有 3 个页面上,我想在单击时链接到每个页面。但是,该链接仅适用于 index.html 页面,一旦在另一个页面上,将不再处理事件。

基本上,当我在第 2 页或第 3 页时,不会处理点击事件。 (使用 console.log 测试)

html:

<div data-role="page" id="article1">
    <div data-role="header" data-theme="a" data-position="fixed">
         <h1>Notes</h1>
    </div>

<div id="global-footer" data-role="footer" data-position="fixed" data-theme="a" data-id="main-footer" class="ui-bar">
     <div id="footer-container">
          <button id="notespagebtn" class="notespagebtn" data-role="none">Notes</button>
          <button id="reminderspagebtn" class="reminderspagebtn" data-role="none">Reminders</button>
          <button id="listspagebtn" class="listspagebtn" data-role="none">Lists</button>
      </div> 
    </div> 

</div>

<div data-role="page" id="article2">
     <div data-role="header" data-theme="a" data-position="fixed">
         <h1>Reminders</h1>
     </div>

<div id="global-footer" data-role="footer" data-position="fixed" data-theme="a" data-id="main-footer" class="ui-bar">
    <div id="footer-container">
        <button id="notespagebtn" class="notespagebtn" data-role="none">Notes</button>
        <button id="reminderspagebtn" class="reminderspagebtn" data-role="none">Reminders</button>
        <button id="listspagebtn" class="listspagebtn" data-role="none">Lists</button>
     </div> 
  </div> 

</div>

<div data-role="page" id="article3">
    <div data-role="header" data-theme="a" data-position="fixed">
        <h1>Lists</h1>
    </div>

   <div id="global-footer" data-role="footer" data-position="fixed" data-theme="a" data-id="main-footer" class="ui-bar">
        <div id="footer-container">
             <button id="notespagebtn" class="notespagebtn" data-role="none">Notes</button>
             <button id="reminderspagebtn" class="reminderspagebtn" data-role="none">Reminders</button>
             <button id="listspagebtn" class="listspagebtn" data-role="none">Lists</button>
   </div> 
</div> 

JS:

$("#notespagebtn").click(function() {
        $.mobile.changePage("#article1");
    });

    $("#reminderspagebtn").click(function() {
        console.log("test");
        $.mobile.changePage("#article2");
    });

    $("#listspagebtn").click(function() {
        console.log("test");
        $.mobile.changePage("#article3");
    });

【问题讨论】:

    标签: javascript jquery html jquery-mobile


    【解决方案1】:

    所有 3 个页面的按钮具有相同的 ID。给他们不同的ID或使用类来绑定事件。

    Jsfiddle

    $(".notespagebtn").click(function() {
        $.mobile.changePage("#article1");
    });
    
    $(".reminderspagebtn").click(function() {
        console.log("test");
        $.mobile.changePage("#article2");
    });
    
    $(".listspagebtn").click(function() {
        console.log("test");
        $.mobile.changePage("#article3");
    });
    

    【讨论】:

    • 谢谢,已经成功了。不能说我理解为什么类选择器而不是 ID 起作用,因为它们都匹配,但我会接受它(我会接受答案)
    猜你喜欢
    • 2020-01-07
    • 2012-12-12
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多