简体   繁体   English

使可点击 <div> 与内部链接,如何仅触发一个动作?

[英]Make clickable <div> with links inside, how to fire just one action?

I have a <div> in my code which I made clickable. 我的代码中有一个<div> ,使我可以点击。 When the <div> gets clicked the height and text expand using the jQuery function $(div).animate(); 单击<div>时,使用jQuery函数$(div).animate();扩展高度和文本$(div).animate(); . This works as I want to, but I am still experiencing one small problem. 这可以按我的意愿工作,但是我仍然遇到一个小问题。

In this there are several links. 在此有几个链接。 Those links all redirect to another page. 这些链接都重定向到另一个页面。 when one of the links get clicked, the div still animates, if load times are a bit slow you see the full animation and then you go to the page of the link. 单击其中一个链接时,div仍会设置动画,如果加载时间有点慢,您会看到完整的动画,然后转到链接页面。 This is not what it has to do. 这不是必须要做的。 If a link iside the div gets clicked, the animation doesn't need to be fired because the user will be redirected. 如果单击div的链接,则无需触发动画,因为将重定向用户。 only when the div itself (not a link inside it) gets clicked, then the animation should fire. 仅当单击div本身(而不是其中的链接)时,才应触发动画。

Any idea how I can achieve this? 知道我该如何实现吗? Is this doable via use of Z-index? 通过使用Z-index可以做到吗?

This is how my div looks: 这是我的div的外观:

<div class="question-summary collapsed unselectable">
        <div class="votes">

            <div class="mini-counts">@item.GetTotalVotes()</div>
            <div>Votes</div>
        </div>

        @if (item.Answers.Count == 0)
        {
            <div class="votes unanswered">
                <div class="mini-counts">@Html.DisplayFor(modelItem => item.Answers.Count)</div>
                <span>Answers</span>
            </div>
        }
        else
        {


           <div class="votes answered">
               <div class="mini-counts">@Html.DisplayFor(modelItem => item.Answers.Count)     </div>
               @if (item.Answers.Count == 1)
               {
                   <span>Answer</span>
               }
               else
               {
                   <span>Answers</span>
               }
           </div>
        }
        <div class="summary">

            <h3 class="link-ontop">@Html.ActionLink(@item.GeneralQuestion, "Details", new { id = item.QuestionId})</h3>
            <p class="extra-info">@item.GetShortExplanation(160) <span class="link-ontop">@Html.ActionLink("Read More...", "Details", new { id = item.QuestionId})</span></p>

            <div class="meta-info">

                @item.DateSubmitted.ToShortDateString()
                @Html.DisplayFor(itemModel => item.Author.FirstName)
            </div>
        </div>

        <div class="tags">
            @foreach (var topic in item.Topics)
            {
                <a href="@Url.Action("IndexByTopic","Question",new { topicId = @topic.TopicId })" class="tag-name">@topic.Name</a>
            }
        </div>

        <div class="no-animate">
            <h1>@item.GeneralQuestion</h1>
            @Html.DisplayWithBreaks(item.Explanation)
            <br />
            <br />

            <a href="@Url.Action("Details", new { id = item.QuestionId })">This question has @item.Answers.Count answers, click here for details!</a>
        </div>
    </div>

This is used in an MVC.NET project, that's why there's Razor syntax in the html code. 这在MVC.NET项目中使用,这就是为什么html代码中包含Razor语法的原因。

use .stopPropagation(); 使用.stopPropagation(); to stop the event bubling: 停止事件冒泡:

$('div a').on('click', function(e){
    e.stopPropagation();
});

As you have bound an event to div so if you click any child element of the div then event tend to bubble up to the dom tree, that is why your div gets the click event to fire its animate function. 由于已将事件绑定到div,因此,如果单击div的任何子元素,则事件往往会冒泡至dom树,这就是为什么div会获得click事件以触发其动画功能的原因。

event.stopPropagation(); is used to stop bubbling the event to the dom tree. 用于停止将事件冒泡到dom树。 so with the above click event you can stop the event to bubble up to its parent which has the animate function bound to it. 因此,使用上述click事件,您可以停止事件使其冒泡到绑定了动画功能的父事件。

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

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