问题
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