Phonegap,如何在单击导航栏按钮时触发函数

Phonegap, how to fire a function when Navbar Button is clicked

本文关键字:按钮 函数 导航 单击 Phonegap      更新时间:2023-09-26

我正在开发一个用于学习目的的应用程序。

有一个标题,内容和带有2个按钮的导航栏。

body="onLoad()" 函数在应用程序启动时运行得非常好,但是当我单击导航栏上的另一个按钮时,我想触发一个显示页面内容的函数。但是当我在新 HTML 文件中调用一个函数时,就像在索引文件(body="contentLoader())上一样"时,它不会触发!

下面是索引中的代码.html

<html>
<head>
<head>
</head>

<body onload="onBodyLoad()">

现在我想点击导航栏上的"列表视图"按钮,它有自己的 HTML 页面,也带有

<body onload="onListLoad()">

但是我的 javascript 函数function onListLoad() { alert("test"); }没有触发。

我不知道为什么。这不是正常的方式吗?

提前谢谢。问候,约翰。

我很

确定你的子页面中的标签在PhoneGap中被去除了。 只有主索引页会触发其 onload 方法。

据我收集,您正在尝试在激活导航选项卡之一后立即触发一些代码。 我不确定我能告诉你"正确"的方法,但这是我所做的:

<div data-role="page" id="reports-browse">
    <div data-role="header"></div>
    <div data-role="navbar">
        <ul>
            <li><a href="#" onclick="$('#tab1').show(); $('#tab2').hide();" class="ui-btn-active">Tab 1</a></li>
            <li><a href="#" onclick="$('#tab1').hide(); $('#tab2').show();">Tab 2</a></li>
        </ul>
    </div>
    <div data-role="content">
        <div id="tab1">This is my first tab</div>
        <div id="tab2" style="display:none">This is my second tab.</div>
    </div>
    <script src="reports.browse.js"></script>
    <script>reports_browse.init();</script>
</div>

然后我的 init() 函数执行以下操作:

$( document ).bind( "pagebeforeshow", function( prev_page ){
     // Populate the two tabs here.
});

希望这有帮助。