是一种形式的外键html标签是否有效? [英] Are button html tags outside of a form valid?

查看:178
本文介绍了是一种形式的外键html标签是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只注意到,在的SoundCloud,跑道上的动作按钮(如,转贴等)都是HTML按钮标记。此外,它们既不是一个形式里面也不绑定到一个表格一拉HTML5也没有提交表单(他们显然正在通过JavaScript处理)。这是有效的HTML?一个按钮可以不用的形式存在?或者这是否只是让这些按钮纯点击的div?而那又是/ unvalid如何有效屏幕阅读器?

I just noticed that, in soundcloud, the "action" buttons on a track (like, repost, etc...) are all html button tags. Moreover, they are neither inside a form nor they bind to a form a la html5 nor submit a form (they apparently are being handled through javascript). Is this valid HTML? Can a button exist without a form? Or does that just make these buttons plain clickable divs? And how valid/unvalid would that be for screenreaders?

推荐答案

A 按钮元素是文档正文文本的地方级标记可能出现在任何地方有效。这样的元素不需要有一个格式元素的任何关系。当前权威的参考是 HTML 4.01规范和正式规则的DTD,但你可以采取一个快捷方式,并使用 W3C标记验证来检查文档是否有效。

A button element is valid anywhere in the document body where text-level markup may appear. Such an element need not have any relationship to a form element. The currently authoritative reference is the HTML 4.01 specification, and the formal rules are in the DTD, but you can take a shortcut and use the W3C Markup Validator to check whether a document is valid.

在不与表单关联,一个按钮元素不是从跨度元素非常不同的(而不是 DIV ,这是块级默认情况下),但按钮具有特殊的默认呈现。浏览器和辅助软件的可能的治疗按钮另有特别呢,这是最安全的方法是使用按钮仅对应该点击时触发一些动作元素。相反,这种元素是使用按钮标记,虽然你可以创建其他的方式太(图像或CSS,大部分)在视觉上类似按钮的元素往往最重presented。

When not associated with a form, a button element is not very different from a span element (rather than div, which is block-level by default), but button has special default rendering. Browsers and assistive software may treat button as otherwise special too, and it is safest to use button only for elements that are supposed to trigger some action when clicked on. Conversely, such elements are often best represented using button markup, though you can create visually button-like elements in other ways too (images or CSS, mostly).

外一种形式,一个按钮元素有类型=按钮默认(而这通常是唯一明智的键入它的话)。这意味着它只能有通过JavaScript的效果。这并不使它以任何方式无效。但是,你可以考虑生成而不是让它们作为静态HTML内容通过JavaScript这样的按钮,这样当脚本被禁用,不会有一个(有效的,但)混乱的按钮,什么也不做。

Outside a form, a button element has type=button as default (and that’s normally the only sensible type for it then). This means that it can only have an effect via JavaScript. This does not make it invalid in any way. However, you may consider generating such buttons via JavaScript instead of having them as static HTML content, so that when scripting is disabled, there won’t be a (valid, but) confusing button that does nothing.

要解决澄清在下面的评论的问题:

To address clarifying questions in the comment below:

A 按钮类型=按钮元素类似于输入类型=按钮;所不同的是,后者没有内容,但需要从属性在按钮上显示的文本,而按钮有可能是富(有标记)的内容。

A button type=button element is similar to input type=button; the difference is that the latter has no content but takes the text shown in the button from the value attribute, whereas button has content that can be "rich" (with markup).

对于任何一个元素,如果使用它们导致服务器操作(通常情况下,通过Ajax调用),那么我们确实可以要求该页面是如何使用禁用JavaScript。但这个问题可能是无关紧要的(也许是页面是无论如何应该用JavaScript运行的应用程序),在任何情况下没有什么的正式的错误的想法。

For either element, if using them causes a server action (typically, via an Ajax call), then we can indeed ask how the page works with JavaScript disabled. But this question might be irrelevant (perhaps the page is an application that is supposed to run with JavaScript anyway), and in any case there is nothing formally wrong with the idea.

他们为什么存在?对于笔者的方便和遗留原因,我会说。同样,有人可能会问,为什么HTML活动属性可言,当他们不可能没有客户端脚本工作,你可以将事件处理程序在JavaScript中的元素。但在初期,这是不可能的,即使在present,它可能是使用按钮更加方便元素或的onclick 属性,而不是做的事情在JavaScript中(以及用于制造元素看起来像一个按钮,CSS)。此外,还有点按钮输入类型=按钮创建要素依赖于浏览器的外观,它可能是认为,对于大多数用户来说,任何有对他的按钮浏览器的默认样式风格被认为是一个按钮。

Why do they exist? For author convenience and for legacy reasons, I would say. Similarly, one might ask why HTML has event attributes at all, when they cannot possibly work without client-side scripting and you can assign event handlers to elements in JavaScript. But in the early days, that was not possible, and even at present, it might be more convenient to use the button element or the onclick attribute than to do things in JavaScript (and, for making an element look like a button, CSS). There is also the point that button and input type=button create browser-dependent appearance for elements, and it might be argued that for most users, anything that has the style of his browser’s default style for buttons is perceived as a button.

这篇关于是一种形式的外键html标签是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆