jQTouch转到下一页时执行javascript代码

时间:2010-09-01 23:47:43

标签: javascript iphone html debugging jqtouch

我一直在开发一款需要动态更改内容的iPhone应用。我有以下代码:

       <div id="home">
       <div class="toolbar">
            <a class="blueButton" href="javascript: void(0);" onClick="$('#logout').show();">Logout</a>
            <h1>AllaccessMD</h1>
        </div>
        <h1>Home</h1>
        <ul class="edgetoedge" id="ulHome"></ul>
    </div>

当调用此页面时,我调用了javascript函数:

function setupHome(){
if(localStorage.role == "Specialist"){
    var homeUL = '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#ePresentations">My E-Presentation</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#date">My Newsletter</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#date">My Events</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#date">Medical Partners</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#date">Search</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#date">Profile</a></li>';
    $('#ulHome').html(homeUL);
} else {
    var homeUL = '<li class="arrow"><a id="m,' + localStorage.userID + '" href="#date">Medical Partners</a></li>'
            + '<li class="arrow"><a id="m,' + localStorage.userID + '" href="#date">Search</a></li>'
            + '<li class="arrow"><a id="m,' + localStorage.userID + '" href="#date">Profile</a></li>';
    $('#ulHome').html(homeUL);
}

$('#ulHome li a').click(function(){
    alert('I dont see this alert on a iPhone!');
    if(this.href.search("#ePresentations") != 0) {
        var idObject = getID(this.id);
        setupEPresentation(idObject.id);
    }
});

}

这一切都正常,除了$('#ulHome li a')。click()不起作用。它适用于Firefox和Chrome,但不适用于iPhone。

我哪里出错了?或者有更好的方法来实现这个吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用点击而不是点击,自动更改页面功能将覆盖您的链接,因为他们有一个href位置。此外,我不会将代码中的javascript与javascript中的事件混合,所以:

   <div id="home">
     <div class="toolbar">
        <a id="logoutButton" class="blueButton" href="#">Logout</a>
        <h1>AllaccessMD</h1>
     </div>
   <h1>Home</h1>
   <ul class="edgetoedge" id="ulHome"></ul>
   </div>

function setupHome(){
if(localStorage.role == "Specialist"){
    var homeUL = '<li class="arrow"><a class="Presentation" id="s,' + localStorage.userID + '" href="#">My E-Presentation</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" data="#">My Newsletter</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#">My Events</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#">Medical Partners</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#">Search</a></li>'
            + '<li class="arrow"><a id="s,' + localStorage.userID + '" href="#">Profile</a></li>';
    $('#ulHome').html(homeUL);
} else {
    var homeUL = '<li class="arrow"><a id="m,' + localStorage.userID + '" href="#">Medical Partners</a></li>'
            + '<li class="arrow"><a id="m,' + localStorage.userID + '" href="#">Search</a></li>'
            + '<li class="arrow"><a id="m,' + localStorage.userID + '" href="#">Profile</a></li>';
    $('#ulHome').html(homeUL);
}

$('#ulHome li a').tap(function(){
    alert('I dont see this alert on a iPhone!');
    if(this.hasClass("Presentations") ) {
        var idObject = getID(this.id);
        setupEPresentation(idObject.id);
    }
});

$('#logoutButton').tap(function() { 
   $('#logout').show();
});