简体   繁体   English

在div上单击任意位置,而不是直接在链接上单击

[英]Click anywhere on div instead of directly on Link

In the scenario shown in the code below, how can I allow the user to click anywhere on div.post , instead of precisely on one of the the actual a elements? 在下面的代码所示的方案中,如何允许用户单击div.post任意位置 ,而不是精确地单击实际a元素之一?

<div class="post" style="padding: 20px; background-color: transparent;">

    <div class="thumbnail" style="float: left; background-color: #ccc;">    
        <a href="http://...">
            <img src="http://...">
        </a>
    </div>

    <div class="title" style="float: right;">
        <a href="http://...">title</a>
    </div>

</div>

I also need to highlight the complete div.post on mouseover, which I currently do using: 我还需要突出显示鼠标悬停时完整的div.post ,我目前正在使用:

[EDIT: Thx to your answers, I have replaced the following by CSS instead of the jQuery] [编辑:谢谢您的回答,我将以下内容替换为CSS,而不是jQuery]

$("div.post").hover(

    function() {
        $(this).css("background-color", "#333");
        $(this).find("div.thumbnail").css("background-color", "#333");
    },

    function() {
        $(this).css("background-color", "transparent");
        $(this).find("div.thumbnail").css("background-color", "#ccc");      
    }
);

EDIT : Solutions will be preferred that: 编辑 :解决方案将是首选:

  • use CSS instead of jQuery 使用CSS代替jQuery
  • validate as XHTML 1.0 Strict 验证为XHTML 1.0 Strict
  • do not wrap an a around a div 不包装的adiv
  • do not involve an empty a 不涉及一个空a

So far, I consider ArVan's solution to be the best (see my comment there). 到目前为止,我认为ArVan的解决方案是最好的(请参阅我的评论)。

Try adding an onclick listener on the div. 尝试在div上添加onclick侦听器。 You can do this in the html like this <div class="post" style="padding: 20px; background-color: transparent;" onclick="location.href='http://...'"> 您可以在html中执行此操作,例如<div class="post" style="padding: 20px; background-color: transparent;" onclick="location.href='http://...'"> <div class="post" style="padding: 20px; background-color: transparent;" onclick="location.href='http://...'"> . <div class="post" style="padding: 20px; background-color: transparent;" onclick="location.href='http://...'">

Or you can do it with jQuery like this: 或者您可以使用jQuery这样来做到这一点:

$("div.post").click(function(){
   window.location.href = "http://...";
});

Why do you need to have the DIVs at all. 为什么您需要所有DIV。 You could simply style the < a > tags accordingly. 您可以简单地相应地设置<a>标签的样式。 Add styles to < a > classes as you desire. 根据需要向<a>类添加样式。 Get rid of inline styles! 摆脱内联样式!

<div class="post">

        <a class="thumbnail" href="http://...">
            <img src="http://...">
        </a>

        <a class="title" href="http://...">title</a>

</div>

You could of course also make the a elements fill up the entire div it's in - and set the background to the a element - so you'll only need js for .post background: 当然,您也可以使a元素填充整个div,并将background设置为a元素-因此,您只需要js作为.post background:

a {
    background:transparent;
    display:block;
    height:100%; /* .post will need a fixed height */
    width:100%;
}
a:hover {
    background:#333;
}

The only reason that you wouldn't want to wrap a <div> tag with an <a> tag is because you're not supposed to put block-level elements inside inline elements. 您不想用<a>标签包装<div>标签的唯一原因是,您不应该将块级元素放入内联元素中。 If you set your containing <a> tag to be a block-level element, then there is no reason why you wouldn't want to wrap the <div> with an <a> tag. 如果将包含的<a>标记设置为块级元素,则没有理由不希望使用<a>标记包装<div>

Also, keep in mind that you can place <a> tags within <a> tags. 另外,请记住,你可以把<a>内标签<a>标签。

<a class="post" href="#" style="padding: 20px;">
    <div class="thumbnail" style="float: left; background-color: #ccc;">    
        <a href="http://...">
            <img src="http://...">
        </a>
    </div>
    <div class="title" style="float: right;">
        <a href="http://...">title</a>
    </div>
</a>

You can choose to keep or remove the links, but all you have to do is use a jQuery click function on the div, and window.location. 您可以选择保留还是删除链接,但是您所要做的就是在div和window.location上使用jQuery click函数。

$("div.post").click(function(){
    window.location.href = "myHrefHere.ext";
});

Super easy! 超级容易!

Heres a DEMO 继承人

$('div > a').each(function() {
    var href = this.href;
    $(this).parent().click(function() { location.href = href; });
});

Or, you could avoid using Javascript at all (which I always prefer when possible), by positioning your link over your entire div.post element, and using CSS hover effects: 或者,可以通过将链接放置在整个div.post元素上并使用CSS悬停效果来完全避免使用Javascript(在可能的情况下,我总是倾向于使用Javascript):

http://jsfiddle.net/jblasco/WXugG/14/ http://jsfiddle.net/jblasco/WXugG/14/

At the very least you should use CSS for changing the background color on hover, rather than Javascript. 至少应该使用CSS来更改悬停时的背景颜色,而不是使用Javascript。

EDIT: Updated the fiddle to include an empty span in the a element, as per Make a div into a link . 编辑:将小提琴更新为在a元素中包括一个空span ,如使div进入链接

LATEST EDIT: Updated the fiddle to use the XHTML 1.0 Strict doctype here , which both works and validates successfully . 最新编辑:更新了小提琴以在此处使用XHTML 1.0 Strict doctype,该文档类型既可以正常工作,也可以成功验证

If you're using HTML5 you can give the anchor tag some children and use CSS accordingly. 如果您使用的是HTML5,则可以给锚标记添加一些子标记,然后相应地使用CSS。 This requires the least amount of css and markup and no JS at all. 这需要最少的css和标记,根本不需要JS。

Check out this example: http://jsfiddle.net/DavidVII/6ErgJ/ 看看这个例子: http : //jsfiddle.net/DavidVII/6ErgJ/

also, this validates using HTML5. 同样,这可以使用HTML5进行验证。 This of course isn't legal in strict. 当然,严格来说这是不合法的。

You can do it just with CSS. 您可以使用CSS来完成。 This is a mix of @ptriek's and @qwertymk's solutions. 这是@ptriek和@qwertymk解决方案的混合。

demo 演示

Remember, if you're going to be floating stuff, you have to clear: both; 请记住,如果您要浮动的东西,则必须clear: both; after it. 之后。 Try removing the clearfix , and see how it's different. 尝试删除clearfix ,看看它有什么不同。

Update: 更新:

For a little extra credit, I replaced the jQuery for hovering with CSS. 为了获得更多的荣誉,我将jQuery悬停在CSS上。 I think it works pretty well. 我认为效果很好。 The demo link above has been updated. 上面的演示链接已更新。

Update 2: 更新2:

Here's a bit of a compromise. 这是一个折衷方案。 A little jQuery is used to let the anchor go inside the div, instead of around it. 一个小的jQuery用于让锚点进入div 内部 ,而不是围绕它。 It all validates as XHTML 1.0 Strict and works fine. 所有这些都通过XHTML 1.0 Strict验证,并且可以正常工作。 I also added cursor: pointer; 我还添加了cursor: pointer; to make it look like a link (you get the usual hand cursor for links) even though it's a div. 使它看起来像一个链接(您通常会看到链接的手形光标),即使它是一个div。

The XHTML looks like this, XHTML看起来像这样,

<div class="post" style="padding: 20px;">
    <a href="http://..." class="placehold" />
    <!-- content goes here -->
</div>

You can still highlight any text inside the div, and right click images (to copy or save-as). 您仍然可以突出显示div中的任何文本,然后右键单击图像(复制或另存为)。

http://jsfiddle.net/HXdA5/4/ http://jsfiddle.net/HXdA5/4/

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

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