官术网_书友最值得收藏!

Implementing jQuery Mobile

Ok, we've got the bits. How do we use them? Adding jQuery Mobile support to a website requires the following three steps at a minimum:

  1. First, add the HTML5 DOCTYPE declaration to the page:
    <!DOCTYPE html>
    

    This is used to help inform the browser about the type of content it will be dealing with

  2. Add a viewport meta tag:
    <meta name="viewport" content="width=device-width, initial-scale=1">
    

    This will help set better defaults for pages when viewed on a mobile device

  3. Finally, the CSS, the JavaScript library, and jQuery itself need to be included into the file.

Let's look at a modified version of our previous HTML file that adds all of these:

Listing 1-2: test2.html
<!DOCTYPE html>
<html>
  <head>
    <title>First Mobile Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="jquery.mobile-1.4.5.min.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script type="text/javascript" src="jquery.mobile-1.4.5.min.js"></script>
  </head>
  <body>
    <h1>Welcome</h1>
     <p>
       Welcome to our first mobile web site.
       It's going to be the best site you've ever seen.
       Once we get some content. And a business plan.
       But the hard part is done!
     </p>
    <p>
      <i>Copyright Megacorp &copy; 2015</i>
    </p>
  </body>
</html>

For the most part, this version is the exact same as listing 1, except for the addition of the DOCTYPE declaration, the CSS link, and our two JavaScript libraries. Notice that we pointed to the hosted version of the jQuery library. It's perfectly fine to mix local JavaScript files and remote ones. If you want to ensure that you can work offline, you can simply download the jQuery library as well.

So, while nothing changed in the code between the body tags, there is going to be a radically different view now in the browser. The following screenshot shows how the iOS mobile browser renders the page now:

Right away, you see a couple of differences. The biggest difference is the relative size of the text. Notice how much bigger and easier to read it is. As we said, the user could have zoomed in on the previous version, but many mobile users aren't aware of this technique. This page loads up immediately in a manner that is much more usable on a mobile device.

主站蜘蛛池模板: 伊金霍洛旗| 晋州市| 松滋市| 循化| 宁化县| 修文县| 天祝| 曲水县| 琼结县| 桃江县| 赞皇县| 阜康市| 花垣县| 广元市| 金乡县| 霍林郭勒市| 乌拉特后旗| 中牟县| 衡山县| 江油市| 宜章县| 文山县| 汝阳县| 宁河县| 凯里市| 双江| 南皮县| 奎屯市| 上饶县| 西青区| 息烽县| 河北区| 安泽县| 巴彦淖尔市| 成都市| 黑山县| 阿拉善右旗| 濮阳县| 大同市| 元阳县| 德阳市|