0

我一直在搜索如何用导航栏替换内容,我发现了这个网站:Navbars 的嵌套内容使用 jQuery Mobile,并且完全无法理解 javascript 是如何工作的。是的,我是这个领域的新手。

所以,不知何故,他设法用这段代码创建了这样的内容:

<div data-role="page">
  <div data-role="header">
    <div data-role="navbar">
      <ul>
        <li><a href="#one" class="ui-btn-active ui-btn-persist">One</a></li>
        <li><a href="#two">Two</a></li>
        <li><a href="#three">Three</a></li>
      </ul>
    </div>
  </div>
  <div data-role="content">
    <div id="one">One content</div>
    <div id="two">Two content</div>
    <div id="three">Three content</div>
  </div>
</div>

(function($) {

// Before handling a page change...
$(document).bind("pagebeforechange", function(e, data)
{
    // If the new page is not being loaded by URL, bail
    if (typeof data.toPage !== "string")
    {
        return;
    }

    // If the new page has a corresponding navbar link, activate its content div
    var url = $.mobile.path.parseUrl(data.toPage);
    var $a = $("div[data-role='navbar'] a[href='" + url.hash + "']");
    if ($a.length)
    {
        // Suppress normal page change handling since we're handling it here for this case
        e.preventDefault();
    }
    // If the new page has a navbar, activate the content div for its active item
    else
    {
        $a = $(url.hash + " div[data-role='navbar']").find("a.ui-btn-active");

        // Allow normal page change handling to continue in this case so the new page finishes rendering
    }

    // Show the content div to be activated and hide other content divs for this page
    var $content = $($a.attr("href"));
    $content.siblings().hide();
    $content.show();
});

})(jQuery);

我打算用我的导航栏创建 3 个内容,这是我的代码:

<div data-role="navbar" data-iconpos="bottom">
      <ul>
       <li><a href="#one" id="home" data-transition="fade" data-theme="" data-icon="home" class="ui-btn-active">Home</a></li>
       <li><a href="#two" id="vmap" data-transition="fade" data-theme="" data-icon="star">V-Map</a></li>
       <li><a href="#three" id="login" data-transition="fade" data-theme="" data-icon="check">Login</a></li>
      </ul>
     </div>
    </div>
    <div data-role="content" align="center" style="margin:2em 1em 2em 1em;">
     <div id="one">First Content</div>
     <div id="two">Second Content</div>
     <div id="three">Third Content</div>
    </div>

我没有像示例给出的上一页,有人可以帮我处理 javascript 吗?

4

1 回答 1

0

确保在代码中引用 jQuery、jQuery Mobile库。我认为上一个按钮的问题是,您没有页面和标题 div 包装器

您必须使用他的确切标记,包括页面和标题 div。

<div data-role="page">
  <div data-role="header">
  <div data-role="navbar">
    <ul>
      <li><a href="#one" class="ui-btn-active ui-btn-persist">One</a></li>
      <li><a href="#two">Two</a></li>
      <li><a href="#three">Three</a></li>
    </ul>
  </div>
  </div>
  <div data-role="content">
    <div id="one">One content</div>
    <div id="two">Two content</div>
    <div id="three">Three content</div>
  </div>
</div>
于 2013-05-25T06:25:00.043 回答