这个HTML有效吗?

时间:2011-08-24 21:54:47

标签: html html5

此代码是否包含任何无效内容。我有一张桌子里面的表格。那好吗?

<form name="myForm" id="myForm">
    <table id="myTab">
        <tr>
            <td>
                <label for="id">User ID:</label>
                <input type="text" id="id" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="pass">Password:</label>
                <input type="password" id="pass" name="pass" />
            </td>
            <td>
                <button type="button" class="submit">Submit</button>
            </td>
        </tr>
        <tr><td><input type="reset" /></td></tr>
    </table>
    <div class="error"></div><div class="correct"></div>
</form>

结果 - http://jsfiddle.net/mBwAh/

4 个答案:

答案 0 :(得分:2)

你没有说出问题是什么,但有一点可以吸引眼球的事实是你每行td的数量不同,而colspan没有<tr> <td colspan="2"> <--- makes column span across three columns in the other rows 就可以了。< / p>

{{1}}

答案 1 :(得分:2)

<form>可以包含文本和标记(段落,列表等),没有列出它可以包含的内容的限制。这是W3C规范的说法:

http://www.w3.org/TR/html4/interact/forms.html#h-17.3

至于你<table>的用法,它是完全有效的HTML,实际上{* 1}}元素在HTML5规范中这是W3C规范:

http://www.w3.org/TR/html5/tabular-data.html#the-table-element

您还需要向<table>添加colspan,其中只包含一个<tr>,您还应该为<td>添加name属性因为没有它就不会做任何事情。

<input>

答案 2 :(得分:2)

<form name="myForm" id="myForm">
    <table id="myTab">
        <tr>
            <td colspan="2">
                <label for="id">User ID:</label>
                <input type="text" id="id" />
            </td>
        </tr>
        <tr>
            <td>
                <label for="pass">Password:</label>
                <input type="password" id="pass" name="pass" />
            </td>
            <td colspan="2">
                <button type="button" class="submit">Submit</button>
            </td>
        </tr>
        <tr><td colspan="2"><input type="reset" /></td></tr>
    </table>
    <div class="error"></div><div class="correct"></div>
</form>

有些colspan失踪了。

您可以在此处查看HTML5有效性的代码:http://validator.w3.org/#validate_by_input

答案 3 :(得分:1)

这可能是“有效”,但您使用表格结构进行布局,这不是一个好主意。如果可能的话,你应该改变你的结构。

<form name="myForm" id="myForm">
    <label for="id">User ID:</label>
    <input type="text" id="id" /><br />
    <label for="pass">Password:</label>
    <input type="password" id="pass" name="pass" /><br />
    <button type="button" class="submit">Submit</button><br />
    <input type="reset" /><br />
    <div class="error"></div><div class="correct"></div>
</form>

希望这有帮助:)