san*_*zer 12 html5 semantic-markup htmlbutton html-heading
我有一个标题元素,需要在单击时触发一些JavaScript.我知道我应该只<a>在页面实际更改时使用标签,并且这<button>是JS功能的首选,但由于某种原因,它只是感觉不对
<h2><button onclick="myFunction();">My Title</button></h2>
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么那种语义不正确.只有我吗?
Ori*_*iol 14
根据W3C Validator,这样做没有问题.
您可以尝试验证此代码:
<!DOCTYPE html>
<html>
<head>
<title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
</head>
<body>
<h2><button onclick="myFunction();">My Title</button></h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
但是,我建议避免使用内联样式.
按钮的实际文本是什么?它是否适合作为自己的标题?
按钮的文本是在onclick上显示的内容的标题
然后我觉得你状态很好.不过,我会提出一个可能让你感觉更好的小建议:
<h2><a href="#content">My Heading</a></h2>
<element id="content"> [your content] </element>
Run Code Online (Sandbox Code Playgroud)
然后onclick在外部javascript文件中附加处理程序.如果您稍后删除它,该链接仍然有效.
| 归档时间: |
|
| 查看次数: |
11412 次 |
| 最近记录: |