【问题标题】:How to link to specific anchor in table with jquery如何使用jquery链接到表中的特定锚点
【发布时间】:2010-04-29 19:54:47
【问题描述】:

我正在使用这个 jquery 插件:

http://www.jankoatwarpspeed.com/post/2009/07/20/Expand-table-rows-with-jQuery-jExpand-plugin.aspx

我在代码中有锚点,例如:

<a name="art" id="art2"></a> Articles

那我怎样才能打开那个特定的行呢?换句话说,当用户单击从另一个页面到此登录页面的链接时,我希望根据锚标记打开相应的行。

提前致谢!

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    在您的 document.ready 中,您可以调用该锚点的点击事件,如下所示:

    $(function() {
      if(document.location.hash != '')
        $(document.location.hash).click();
    });
    

    这将使www.yoursite.com/yourPage.htm#art2 执行单击该链接导致它打开。只需确保上面的代码您的点击事件处理程序之后,它实际上会在点击时执行一些操作:)

    如果您使用命名锚点登陆#art,类似的方法:

    $(function() {
      if(document.location.hash != '')
        $('a[name="' + document.location.hash.replace('#','') + '"]').click();
    });
    

    【讨论】:

    • @Joel - 是的,我相信这在所有浏览器中都是一致的。
    • 我无法让它工作。在这里做了一个测试页面:libraries.uc.edu/tests/table_rows.html 这样你的例子就是:libraries.uc.edu/tests/table_rows.html#art2
    • @LisaH - 它目前在点击处理程序$("#report tr.odd").click(function(){ 内,需要将其移到外面,并且不需要在它周围使用$(function() { });,因为您已经在 document.ready 中: )
    猜你喜欢
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多