我应该在这里使用标签还是标签?[英]Should I use a tag or tag here? 本文翻译自  Cheetah  查看原文  2012-02-20  836    css/ html/

时间:2022-11-19 14:53:24

I feel rather silly actually asking this question as I am sure it will simply be opinion rather than correctness that will prevail.

我觉得问这个问题很傻,因为我相信这只是一种观点,而不是正确的观点。

But I have a "button" which is fixed to the lower right hand side of the screen which the user can click on to open the chat box.

但是我有一个“按钮”,它固定在屏幕的右下角,用户可以点击它来打开聊天框。

Currently I just have this:

目前我只有这个:

<div id="chatbtn">Sales help online</div>

< div id = " chatbtn " >销售帮助在线< / div >

I chose a <div> tag so that search engines don't think its a main text element. Should I have chosen a <p> tag to be semantically correct here?...or maybe even an <input type="button">

我选择了一个

标签,这样搜索引擎就不会认为它是一个主要的文本元素。我是否应该选择一个

标签在语义上是正确的?或者是

What do you think?

你怎么认为?

3 个解决方案

#1


4  

Button has its own tag actually:

按钮其实有自己的标签:

<button type="button" id="chatbtn">Sales help online</button>

Default type is sometimes submit so best practice is to always say "it's ordinary button, not submit".

默认类型有时是submit,所以最佳实践是总是说“它是普通的按钮,而不是submit”。

If you need to treat it as block element you can either place it inside containing <div> or manually change its style to display: block;.

如果您需要将其视为块元素,您可以将其放置在包含

或手动更改其样式以显示:块;

Referring something you said, <input type="button"> is surely not proper in your case, as it's form element and you don't have any form here thus the "pure" button tag is more proper.

引用您所说的,在您的示例中肯定不合适,因为它是form元素,并且您在这里没有任何表单,因此“纯”按钮标记更合适。

#2


2  

Always try to use the semantic correct element. If you wish to create a button, use the <button> element. If you wish to display a paragraph, use <p>. If you want to group elements in a container without semantic information provided to this container, use <div>.

总是尝试使用语义正确的元素。如果您希望创建一个按钮,请使用

#3


0  

For the button itself, you can use input type=button if there is nothing special in it (as here), or button type=button if you need richer formatting (e.g., an image or a line break inside the button).

对于按钮本身,您可以使用input type=button(如这里所示),如果需要更丰富的格式(例如,图像或按钮中的断行),则可以使用button type=button。

As a wrapper element outside it, use div if the button just needs to appear on a line of its own, p if you additionally wish to have empty space above and below (in default non-CSS rendering). There is no real semantic difference between these elements; in HTML specifications and usage, p means a paragraph in a loose sense only (see e.g. the HTML5 description of p).

作为外部的包装器元素,如果按钮只需要显示在它自己的行上,则使用div,如果您希望在上面和下面有空格(在默认的非css呈现中),则使用p。这些元素之间没有真正的语义差异;在HTML规范和用法中,p仅表示松散意义上的段落(参见HTML5对p的描述)。

In either case, since the button is presumably JavaScript-driven and does not do anything when JavaScript is disabled, it would be better to generate the element (and perhaps the enclosing element as well) dynamically in JavaScript, instead of having it as a static part of the HTML document.

在这两种情况下,由于该按钮可能是JavaScript驱动的,并且在禁用JavaScript时不做任何事情,所以最好在JavaScript中动态地生成元素(可能还有封装元素),而不是将其作为HTML文档的静态部分。

#1


4  

Button has its own tag actually:

按钮其实有自己的标签:

<button type="button" id="chatbtn">Sales help online</button>

Default type is sometimes submit so best practice is to always say "it's ordinary button, not submit".

默认类型有时是submit,所以最佳实践是总是说“它是普通的按钮,而不是submit”。

If you need to treat it as block element you can either place it inside containing <div> or manually change its style to display: block;.

如果您需要将其视为块元素,您可以将其放置在包含

或手动更改其样式以显示:块;

Referring something you said, <input type="button"> is surely not proper in your case, as it's form element and you don't have any form here thus the "pure" button tag is more proper.

引用您所说的,在您的示例中肯定不合适,因为它是form元素,并且您在这里没有任何表单,因此“纯”按钮标记更合适。

#2


2  

Always try to use the semantic correct element. If you wish to create a button, use the <button> element. If you wish to display a paragraph, use <p>. If you want to group elements in a container without semantic information provided to this container, use <div>.

总是尝试使用语义正确的元素。如果您希望创建一个按钮,请使用

#3


0  

For the button itself, you can use input type=button if there is nothing special in it (as here), or button type=button if you need richer formatting (e.g., an image or a line break inside the button).

对于按钮本身,您可以使用input type=button(如这里所示),如果需要更丰富的格式(例如,图像或按钮中的断行),则可以使用button type=button。

As a wrapper element outside it, use div if the button just needs to appear on a line of its own, p if you additionally wish to have empty space above and below (in default non-CSS rendering). There is no real semantic difference between these elements; in HTML specifications and usage, p means a paragraph in a loose sense only (see e.g. the HTML5 description of p).

作为外部的包装器元素,如果按钮只需要显示在它自己的行上,则使用div,如果您希望在上面和下面有空格(在默认的非css呈现中),则使用p。这些元素之间没有真正的语义差异;在HTML规范和用法中,p仅表示松散意义上的段落(参见HTML5对p的描述)。

In either case, since the button is presumably JavaScript-driven and does not do anything when JavaScript is disabled, it would be better to generate the element (and perhaps the enclosing element as well) dynamically in JavaScript, instead of having it as a static part of the HTML document.

在这两种情况下,由于该按钮可能是JavaScript驱动的,并且在禁用JavaScript时不做任何事情,所以最好在JavaScript中动态地生成元素(可能还有封装元素),而不是将其作为HTML文档的静态部分。