如何使一个完整的'div'可点击在HTML和CSS没有javascript? [英] How to make a whole 'div' clickable in html and css without javascript?

查看:143
本文介绍了如何使一个完整的'div'可点击在HTML和CSS没有javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使整个div可以点击,并且在没有javascript和有效代码/标记的情况下点击链接到另一个页面。



如果我有这是我想要的结果做 -

 < a href =#> 
< div>这是一个链接< / div>
< / a>

W3C验证器说,块元素不应放在内联元素中。是否有更好的方法?

解决方案


没有JavaScript和
有效的代码,这是可能的吗?


Pedantic answer:No。



由于你已经加入了另一个注释,因此在 a div $ c>标签。



但是,没有什么能阻止您使标签的行为非常类似于 div ,但不能在其中嵌入其他块标记。如果它适合你的标记,设置 display:block 在你的一个标签和大小/浮动你喜欢。 / p>

如果你违反你的问题的前提,你需要避免javascript,因为其他人已经指出我们可以使用onClick事件处理程序。 jQuery是一个流行的选择,使这个容易和可维护。


I want to make it so that a whole div is clickable and links to another page when clicked without javascript and with valid code/markup.

If I have this which is what I want the result to do -

<a href="#">
<div>This is a link</div>
</a>

The W3C validator says that block elements shouldn't be placed inside an inline element. Is there a better way to do this?

解决方案

a whole div links to another page when clicked without javascript and with valid code, is this possible?

Pedantic answer: No.

As you've already put on another comment, it's invalid to nest a div inside an a tag.

However, there's nothing preventing you from making your a tag behave very similarly to a div, with the exception that you cannot nest other block tags inside it. If it suits your markup, set display:block on your a tag and size / float it however you like.

If you renege on your question's premise that you need to avoid javascript, as others have pointed our you can use the onClick event handler. jQuery is a popular choice for making this easy and maintainable.

这篇关于如何使一个完整的'div'可点击在HTML和CSS没有javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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