2012-07-29 84 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 

});