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

Time for action – setting up the HTML file

Perform the following steps to get the HTML file set up for our FAQ page:

  1. We'll get started with our sample HTML file and associated files and folders, like we set up in the questions hang into the left margin, making them easy to scan. Inside the <body> tag of your HTML document, add a heading and a definition list as shown in the following code:
    <h1>Frequently Asked Questions</h1>
    <dl>
      <dt>What is jQuery?</dt>
      <dd>
        <p>jQuery is an awesome JavaScript library</p>
      </dd>
    
      <dt>Why should I use jQuery?</dt>  
      <dd>
        <p>Because it's awesome and it makes writing JavaScript faster and easier</p>
      </dd>
      
      <dt>Why would I want to hide the answers to my questions?</dt>
      <dd>
        <p>To make it easier to peruse the list of available questions - then you simply click to see the answer you're interested in reading.</p>
      </dd>
      
      <dt>What if my answers were a lot longer and more complicated than these examples?</dt>
      <dd>
        <p>The great thing about the &lt;dd&gt; element is that it's a block level element that can contain lots of other elements.</p>
        <p>That means your answer could contain:</p>
        <ul>
          <li>Unordered</li>
          <li>Lists</li>
          <li>with lots</li>
          <li>of items</li>
          <li>(or ordered lists or even another definition list)</li>
        </ul>
        <p>Or it might contain text with lots of <strong>special</strong> <em>formatting</em>.</p>
        <h2>Other things</h2>
        <p>It can even contain headings. Your answers could take up an entire screen or more all on their own - it doesn't matter since the answer will be hidden until the user wants to see it.</p>
      </dd>
      
      <dt>What if a user doesn't have JavaScript enabled?</dt>
      <dd>
        <p>You have two options for users with JavaScript disabled - which you choose might depend on the content of your page.</p>
        <p>You might just leave the page as it is - and make sure the &lt;dt&gt; tags are styled in a way that makes them stand out and easy to pick up when you're scanning down through the page. This would be a great solution if your answers are relatively short.</p>
        <p>If your FAQ page has long answers, it might be helpful to put a table of contents list of links to individual questions at the top of the page so users can click it to jump directly to the question and answer they're interested in. This is similar to what we did in the tabbed example, but in this case, we'd use jQuery to hide the table of contents when the page loaded since users with JavaScript wouldn't need to see the table of contents.</p>
      </dd>
    </dl>
  2. You can adjust the style of the page however you'd like by adding in some CSS styles. The following screenshot shows how the page is styled in the example code included with the book:

    For users with JavaScript disabled, this page works fine as is. The questions hang into the left margin and are bolder and larger than the rest of the text on the page, making them easy to scan.

What just happened?

We set up a basic definition list to hold our questions and answers. The default style of the definition list lends itself nicely to making the list of questions scannable for site visitors without JavaScript. We can enhance that further with our own custom CSS code to make the style of our list match our site.

Note

As this simple collapse-and-show (or accordion) action is such a common one, two new elements have been proposed for HTML5: <summary> and <details> that will enable us to build accordions in HTML without the need for JavaScript interactivity. However, at the time of writing this, the new elements are only supported in Webkit browsers, which require some finagling to get them styled with CSS, and are also not accessible. Do keep an eye on these new elements to see if more widespread support for them develops. You can read about the elements in the HTML5 specs (http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html). If you'd like to understand the elements better, the HTML5 Doctor has a great tutorial that explains their use and styling at http://html5doctor.com/the-details-and-summary-elements/.

主站蜘蛛池模板: 南投县| 高雄市| 新干县| 且末县| 宁城县| 荥阳市| 桃江县| 云梦县| 红原县| 台前县| 会昌县| 抚宁县| 贵州省| 陆丰市| 格尔木市| 库车县| 华安县| 武陟县| 永新县| 鹿泉市| 和平县| 孟州市| 沁水县| 云南省| 通州市| 色达县| 绍兴市| 芦山县| 乌拉特前旗| 仙桃市| 秦皇岛市| 遂昌县| 凤山县| 太仆寺旗| 安岳县| 昌黎县| 土默特右旗| 陇川县| 刚察县| 榆社县| 沈阳市|