Jquery mobile: how to execute custom jquery code in page

懵懂的女人 提交于 2019-12-11 06:24:30

问题


I am currently developing a mobile site using jQuery mobile.

I have one simple piece of jQuery that i would like to execute on a button click

eg: Click button, show div. This works fine on the first page, or any page which is directly accessed by url. However, wherever there is page history (ie url contains the a #) the custom snippet of jQuery simply doesn't execute. I'm assuming it's something to do with the location.hash

I've had a good trawl through the jQuery mobile docs and google but can't find any reference to this issue.

Any ideas how i can get this piece of jQuery to execute at all times?


回答1:


Create in your JS code a function that you want to execute(let's call it function savePerson()). Create the button with:

<button onclick="savePerson()">Save</button>



回答2:


For run the custom scripts on jquery mobile page you can follow this link: http://jquerymobile.com/demos/1.0a3/#docs/api/globalconfig.html

I recommend wich you run your code inside a "delegate" event, how[1]:

$(“body”).delegate(“div[data-role*='page']“, “pageshow”, function(){

    // Your code here. It is good to check to not run unnecessary code

});

[1]http://www.raelmax.com/2011/03/06/minha-experiencia-com-jquery-mobile/



来源:https://stackoverflow.com/questions/5245404/jquery-mobile-how-to-execute-custom-jquery-code-in-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!