What is the best way to customize elements with jquery mobile

半城伤御伤魂 提交于 2021-02-07 09:57:01

问题


When using jQuery Mobile .js along with jQuery Mobile .css, what is the best way to customize the default styling such as a link button?

Using jQM, a simple link can be turned into a button by using the following code:

<a href="index.html" data-role="button">Link button</a>

data-role="button" allows jQM to add classes to the link so it can be styled into mobile button touch abled like so:

<a href="index.html" data-role="button" data-corners="true" data-shadow="true" 
data-iconshadow="true" data-wrapperels="span" data-theme="c" class="ui-btn 
ui-shadow ui-btn-corner-all ui-btn-up-c"><span class="ui-btn-inner 
ui-btn-corner-all"><span class="ui-btn-text">Link button</span></span></a>

Is it OK to actually edit the jQM css file for example the ui-btn-up-c class? Or is it better to override the styles somehow, perhaps in an external stylesheet?

I have a couple of concerns. I am wondering if it's possible to break some of the functionality by directly editing jQM.css as jQM seems to use the stylesheet heavily.

Also will it be a problem on updating? Do jQM release a new stylesheet when a new version comes out which would override my edits to the main jQM stylesheet?

Basically what I am asking is how do I edit the jQuery Mobile built in theme?

Thanks and look forward to your answers :)


回答1:


Intro

If you want to change classic jQuery Mobile CSS everything depends on what do you want to do.

Theme roller

Classic way would be to create a completely new set of theme's or add them to existing ones. It is done through jQuery Mobile theme roller. When you open it it will automatically create 3 themes you can then modify as you wish. Or you can Import your current theme CSS and add several more themes (this is probably best solution if you want to change complete look).

Custom CSS changes

This solution requires a little bit of finesse. First if possible NEVER change original CSS unless you are 100% sure what you are doing. For example if you change default button classes it will also affect other widgets that use button classes and there are a lot of them.

Correct way would be to change single/multiple elements with custom CSS file. This way original CSS files is intact and new one can be changed / removed at any time. To do this you will need to use Chrome Webmaster tools or additional plugin called Firebug (for Chrome and FireFox). There are several more solutions but this two are most commonly used.

Problem to think about

Not all is well in this solution. For example, classic a tag button can be easily modified cause that same a tag will stay as a parent of a future styled jQuery Button. But, if your button is created from input tag, like this:

<input type="text" value="Some value" id="change-me"/>

you cant use #change-me id to correct its CSS. Mainly because this input is not a parent tag for a future button, it will be a inner part of a button when jQuery Mobile styles it. It will look like this:

<div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-c">
    <input type="text" id="change-me" value="Some value" class="ui-input-text ui-body-c"/>
</div>

To fix this wrap that input with another div element. Move id from input to div element and then use it to change inner CSS styles.

How to correctly override CSS styles

This is one of a most common questions in this StackOverflow group. When changing predefined CSS rules you must use !important keyword. Changes will usually not work without it. For example if you want to change input button style background (from a previous example, wrapped in a div) you would do it like this:

#change-me .ui-input-text {
    background-color: red !important;
}

If nothing else works change original CSS file(s)

jQuery Mobile can have 1 or 2 CSS files. When using one file both theme and structure is included, or they can be separated into two files. This is useful if you want to change CSS directly. Theme CSS can be easily imported and exported into theme roller without affecting structure CSS file.

One last thing, some things can only be changed by modifying original structure CSS file. For example jQuery Mobile uses a horrible blue glow effect to show when some element has been pressed. It can be removed only directly from structure CSS file.




回答2:


The solution lies in CSS specificity within your own additional CSS file. All you need to do to override any formatting in JQM is to first apply an id to the element you wish to override JQM formatting with your own CSS.

Next, in your own CSS, specify that the class be applied to the id of the container.

As an example, I'll remove the JQM border from an image link below.

#img_button_1 .ui-btn-inner {border: none !important;}

Where #img_button_1 is the id of the HTML anchor element.

<a id="img_button_1" data-role="button" data-theme="none" data-corners="false" data-shadow="false" data-inline="true"
   href="http://www.google.com" target="_blank"><img src="http://www.google.com/images/srpr/logo1w.png" alt="Google" />
</a>

It's as simple as that.

One more important thing, and that is that load order of the external CSS files is significant, and you will want to load your own CSS after JQM CSS.

I have forked a working example at jsFiddle below. http://jsfiddle.net/Z8Xnx/14/

The biggest benefit with this approach, is you do not have to alter the JQM CSS at all, and can leave it alone. This becomes important if your want to import your JQM back into the ThemeRoller tool at a later date. If you modify the actual JQM CSS by hand, you may have an issue successfully importing your JQM back into ThemeRoller again.

I have successfully used this approach to resolve every JQM CSS conflict I have run across since figuring out this specicivity requirement issue. Hope this helps everyone with an easy solution to their JQM style conundrums.

** UPDATE **

It has been noted to me that this method does not work with the latest version of JQM (1.3.0b1), and that is not correct. I have investigated and found this to be a problem with the implementation of this version of JQM at jsFiddle. To prove this, I have put up an example page on my own space with the exact same code as that shown in the jsFiddle example. This means as of my writting, you really can't trust anything at jsFiddle using the lastest version of JQM from the options. Just a heads up, and you can find the working implementation at...

jQuery Mobile CSS Override Example




回答3:


If you are looking to simply change the styling then you can use the jQuery Mobile themeroller.

http://jquerymobile.com/themeroller/index.php

Otherwise, I would suggest using another stylesheet rather than directly editing the jQuery mobile stylesheet.

If you are looking to reduce the number of files that you are serving to your visitors then I would compress both stylesheets and then just insert your styles below their styles as a production copy. That way, you can keep them compressed and combined for production, but you could keep them separate for easy upgrading later and for development ease of use.



来源:https://stackoverflow.com/questions/17563348/what-is-the-best-way-to-customize-elements-with-jquery-mobile

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