【问题标题】:Hyperlink Rewriting with Javascript/Jquery使用 Javascript/Jquery 重写超链接
【发布时间】:2009-04-10 16:20:08
【问题描述】:

这就是我正在尝试做的事情。我的页面上有这样的超链接:

<a href="http://www.wikipedia.com/wiki/Whatever_Page_Here">Whatever Page Here</a>

当用户点击链接时,我想抓住该点击。如果它是一个链接,我不想让他们点击(比如与http://www.wikipedia.com/wiki/xxxxx 格式不匹配的东西)我想弹出一个消息框。否则,我想通过我的 Mvc 控制器将它们汇集起来。像这样:

//Category is the "Whatever_Page_Here" portion of the Url that was clicked.
public ActionResult ContinuePuzzle(string category)
{

}

有什么建议吗?

【问题讨论】:

  • 我已经很久没有看到有人使用“超链接”这个词了...
  • 我用它,因为它很复古。很怀旧。我也喜欢提及其他事物,例如万维网。

标签: javascript jquery asp.net-mvc


【解决方案1】:

从拦截所有点击事件开始:

$(function() {
    $("a").click(ClickInterceptor);
});

function ClickInterceptor(e)
{
    // code to display popup or direct to a different page
    if (redirect)
    {
        var href = $(this).attr("href").split("/");
        location.href = "http://mydomain.com/controller/" + href[href.length - 1];
    }

    return false;
}

"return false;"告诉锚点不要触发导航。

【讨论】:

  • 太棒了!这正是我需要的。但是如何重定向它们?
【解决方案2】:

如果您想选择所有不以“http://http://www.wikipedia.com/wiki/”开头的 a 标签,您可以使用:

$("a").not("a[href^='http://http://www.wikipedia.com/wiki/']")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    相关资源
    最近更新 更多