放置一个 <button> 在语义上是否有效?在 <h2> 里面? [英] Is it semantically valid to put a <button> inside a <h2>?
问题描述
我有一个标题元素,需要在点击时触发一些 JavaScript.我知道我应该只在页面实际更改时使用 标签,并且
s 是 JS 功能的首选,但出于某种原因它只是觉得做错了
I have a header element which needs to fire off some JavaScript when clicked. I know I should only use <a>
tags when the page is actually changing, and that <button>
s are preferred for JS functions, but for some reason it just feels wrong to do
<h2><button onclick="myFunction();">My Title</button></h2>
我不明白为什么这在语义上不正确.只有我吗?
I can't put my finger on why that doesn't feel semantically correct. Is it just me?
推荐答案
按钮的实际文本是什么?它是否适合作为它自己的标题?
What is the actual text for your button? Would it be appropriate as a heading on it's own?
按钮的文本是点击时显示的内容的标题
the text for the button is the title of the content that's being revealed onclick
那我觉得你的状态很好.不过,我会提出一个小建议,让您感觉好些:
Then I think you're in good shape. I will however make a small suggestion that might make you feel better:
<h2><a href="#content">My Heading</a></h2>
<element id="content"> [your content] </element>
然后在外部 javascript 文件中附加一个 onclick
处理程序.如果您稍后将其删除,该链接仍然有效.
Then attach an onclick
handler in an external javascript file. If you remove it later the link will still be valid.
这篇关于放置一个 <button> 在语义上是否有效?在 <h2> 里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!