问题
Im using jquery mobile and trying to remove some classes instead of writing up a bunch css. When I test it in jsFiddle it works fine but not when I view it in the browser. Ive put the script inside the data-role="page" so it should of loaded correctly. Ive also tried .trigger("updatelayout"); but that did not seem to work. Any help would be much appreciated since this should have been a 2 minute task turn into 2 hour headache.
jquery:
$("#panelforminput div").removeClass("ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-c");
$("#newdoctorDiv ul").removeClass("ui-shadow");
jsFddle:
http://jsfiddle.net/adam123/9XXcj/6/
here's the markup from firebug:
<span id="panelforminput" class="ui-li-aside">
<div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-c ui-mini">
<input id="adddocFirstName" class="ui-input-text ui-body-c" type="text" data-mini="true"
placeholder="John" value="" name="adddocFirstName">
</div>
</span>
回答1:
You need to put the script in document.ready in your code to make the elements available to your script also make sure you included jQuery successfully, read how jQuery works.
In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code, Reference.
回答2:
jQuery Mobile - .ready() vs page events
For jQuery Mobile refrain from using .ready() in your code. Stick to jQuery Mobile events http://api.jquerymobile.com/category/events/
In your case, you can use pagebeforeshow or pageshow. Hence, your code should look like this.
$('.selector').on('pagebeforeshow', function () {
// code
});
回答3:
$(document).ready(function(){
$("#panelforminput div").removeClass("ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-
c");
$("#newdoctorDiv ul").removeClass("ui-shadow");
});
The jQuery code you need to write with in the ready function only.
SEE HERE
来源:https://stackoverflow.com/questions/16253300/removeclass-working-on-jsfiddle-but-not-in-the-browser