简体   繁体   English

带有嵌套div的可点击div

[英]Clickable div with nested divs inside

I am looking to make a clickable div that has further divs nested inside. 我正在寻找一个可点击的div,其中嵌入了更多的div。 From what I understand, if you style an anchor tag display:block, it acts as a block (div) and you can nest the div you want clickable within it. 根据我的理解,如果你设置一个锚标签display:block,它就像一个块(div),你可以将想要点击的div嵌套在其中。

However, I don't believe you can continue nesting further divs beyond that... if this is the case, why is that? 但是,我不相信你可以继续嵌套更多的div ...如果是这样的话,那为什么呢?

And also, what would be the best/simplest solution to solve this. 而且,解决这个问题的最佳/最简单的解决方案是什么。 It would ideally be all CSS/HTML, but a jQuery solution be okay if CSS/HTML truly aren't possible. 理想情况下,它将是所有CSS / HTML,但如果CSS / HTML真的不可能,那么jQuery解决方案就可以了。

Anyway... example: 无论如何...例子:

This would work (yes?) as long as the anchor is styled display:block... 这可以工作(是吗?)只要锚定样式显示:块...

enter code here 在这里输入代码

<a>
     <div id='first'>
     </div>
</a>

This would not... 这不会......

enter code here 在这里输入代码

<a>
     <div id='first'>
          <div id='inside first'>
          </div>
          <div id='inside first 2'>
          </div>
     </div>
</a>

Any and all help appreciated. 任何和所有帮助表示赞赏。 Thanks! 谢谢!

Sure your second variant will work - http://jsfiddle.net/W5jG8/ You can go even deeper. 当然你的第二个变体将起作用 - http://jsfiddle.net/W5jG8/你可以更深入。 In fact there is no limit how deep you can go with nesting. 事实上,嵌套的深度是没有限制的。 (Or maybe there is but it's not relevant to real life cases) (或者可能存在,但它与现实生活中的情况无关)

But it's semantically incorrect to nest block level elements into originally inline ones before HTML5. 但是在HTML5之前将块级元素嵌套到最初的内联元素中在语义上是不正确的。 One solution is to replace all <div> -s with <span> -s and also make them block. 一种解决方案是用<span> -s替换所有<div> -s,并使它们阻塞。

Or you can use <div> -s with jQuery and attach a click listener to the wrapper - http://jsfiddle.net/W5jG8/ 或者你可以使用<div> -s和jQuery并将一个点击监听器附加到包装器 - http://jsfiddle.net/W5jG8/

if you are using HTML5 Doctype it's valid to use anchor tags for wrapping div elements, supposing you are not using HTML5 Doctype you can also try this: 如果您使用HTML5 Doctype ,使用锚标签包装div元素是有效的,假设您没有使用HTML5 Doctype您也可以尝试这样做:

div#first {
  cursor: pointer;
}

$('#first').click(function() {
    window.location = 'http://www.example.com'
})

The simplest way is to use jquery, to specify an Id for the div you want to make clikable like so : 最简单的方法是使用jquery,为你想要clikable的div指定一个id,如下所示:

$('#div_Id').click(function(){

blablabla...

});

Here's somme doc : http://api.jquery.com/click/ 这是somme doc: http ://api.jquery.com/click/

Putting div tags inside an anchor tag is not valid html so you shouldn't be doing that. 将div标签放在锚标签内是无效的html所以你不应该这样做。 In your case, the easiest would be to use a little JavasScript and define a simple onclick event on your parent div such as: 在您的情况下,最简单的方法是使用一点JavasScript并在父div上定义一个简单的onclick事件,例如:

 <div id='first' onclick="javascript:MyRedirectFunction()">
      <div id='inside first'>
      </div>
      <div id='inside first 2'>
      </div>
 </div>

Then if you want the user to understand that this div will be clickable, you can use CSS to give it an anchor-like behavior such as: 然后,如果您希望用户了解此div可以被点击,您可以使用CSS为其提供类似锚的行为,例如:

div#first{ cursor: pointer; }
div#first:hover{ background-color:#CCCCCC;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM