2012-07-29 63 views
0

我创建了一个按钮,其中包含一个onclick事件,该事件运行一个提醒"hi"的函数。 onclick事件仅适用于刷新页面的情况。Jquery Mobile Onclick事件在刷新前不起作用

下面是函数:

function testme(){ 
    alert("test"); 
} 

这里是onclick事件:

<a href="#" id="someid" onClick="testme()">Button</a> 

我怎样才能让而无需刷新页面onclick事件的工作?

+0

看看这个问题:http://stackoverflow.c om/questions/5676258/jquerymobile-add-click-event-to-a-button-instead-of-changing-page – spookycoder 2012-07-29 17:51:11

回答

1

如果这是你所做的一切,你可以尝试不使用函数。

<a href="#" id="someid" onClick="alert('hi')">Button</a> 

您也可以尝试jQuery中添加一个点击事件

$(document).ready(function(){ 
$('#someid').click(function(event){ 
    event.stopImmediatePropagation(); 
    event.preventDefault(); 
    alert('hi') 
}); 
}); 

编辑1

从问题spookycoder建议:

你也可以在一个使用rel="external"标签,这将允许它在jQuery Mobile页面上正常执行。

+0

是的,但我还有一些其他功能的值和长功能。我必须使用调用函数。你的分割工作。但如果我正在调用一个函数而不是警告它不起作用。 我该怎么办? 我需要调用函数。 警报工作正常,但没有调用函数。 – Apo 2012-07-29 18:13:23

+0

然后我建议你使用点击事件。 – Shawn31313 2012-07-29 18:14:32

+0

是的,但它不工作,当我试图调用一个功能来做点什么 – Apo 2012-07-29 18:16:13

0

这个工作对我来说(尤其是用iPhone/iPad):

<a href="javascript:testme()" id="someid">Button</a> 
0

我得到了这个问题的解决方法,通过改变我打开我的网页的方式。 我把target =“_ self”放入href元素中,所以它不会使用#系统加载 - 这样,javascript会在从一个页面导航到另一个页面时加载初始页面加载。

  • 我会在我的index.html页面上放置下面的链接,它将导航到我的signup.html页面。

<a href="signup.html" target="_self" data-role="button" data-icon="arrow-r" data-iconpos="notext" data-theme="a" data-inline="true">Signup</a>

注意:您将失去 '花哨' jQuery Mobile的页面转换功能

0

我认为你必须使用:的

$(document).on('pageinit',function(){ 

//Some Code 

}); 

代替:

$(document).ready(function(){ 

//Some Code 

});