简体   繁体   English

使用jQuery单击div时重定向到Html.ActionLink

[英]Redirect to Html.ActionLink when click div using jquery

i want to do clickable div and when clicked redirect to Html.ActionLink lockated inside said div. 我想做可点击的div,当单击时重定向到Html.ActionLink锁定在所述div内。

this is the clickable div jquery 这是可点击的div jquery

<script language="javascript">
    $(".linkdiv").click(function () {
        if ($(this).find("a").length) {
            window.location.href = $(this).find(".top-menu-link").attr("href");
        }
    });
</script>

and this is how i have written the div: 这就是我写div的方式:

<div id="top-menu-element" class="linkdiv">
     @Html.ActionLink("Home", "Index", "Home", new { @class = "top-menu-link" })
</div>

i think that the .attr("href") should be something diffrent but i didn't manage to find out what it should be 我认为.attr(“ href”)应该有所不同,但我没有找出应该是什么

If the script is in the Razor view, just use: 如果脚本在“剃刀”视图中,则只需使用:

window.location.href = '@Url.Action("Index", "Home")';

Which Url.Action does the same thing. 哪个Url.Action执行相同的操作。

<script language="javascript">
    $(".linkdiv").click(function () {
        var anchor = $(this).find("a");
        if (anchor.length) {
            anchor.trigger("click"); 
        }
    });
</script>

Just call the click event for the anchor and use the normal flow of the anchor tag. 只需调用锚点的click事件并使用锚点标记的正常流程即可。 If this doesn't work try to put this bind click event to the anchor: 如果这样不起作用,请尝试将此绑定单击事件放到锚点上:

$(".linkdiv").find("a").click( function (e) {
  window.location.href = this.href;
});

Or more simple replace: 或更简单的替换:

anchor.trigger("click"); 

With: 带有:

anchor.get(0).click();

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

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