简体   繁体   English

克隆后跳转到锚标记

[英]Jump to anchor tag after clone

I have these hyperlink which will jump to anchor tags in UL some where 我有这些超链接,这些超链接将跳转到UL在某些地方的锚标签

<a href="#A">A</a>
<a href="#B">A</a>
<a href="#C">A</a>

<ul>
<li><a name="A"></a></li>
<li><a name="B"></a></li>
<li><a name="C"></a></li>
</ul>

This is to make sure I jump to the right alphabetical letter in the list (which is long and will have scroller). 这是为了确保我跳到列表中正确的字母(很长,并且会有滚动条)。 The problem I have is that this is clone when document ready (requirement of the website for different purpose - cannot change here). 我的问题是,准备好文档后这是克隆文件(出于不同目的要求网站-此处不能更改)。 So after the clone there are 2 sets of anchor tags doing the same thing. 因此,在克隆之后,有2组锚标签执行相同的操作。 I can change the ID of on the clone but not the inner . 我可以更改克隆的ID,但不能更改内部的ID。 The result I want is that when click on A or B or C, it will make the jump in the new clone instead 我想要的结果是,当单击A或B或C时,它将跳转到新克隆中

How to solve this problem? 如何解决这个问题呢? If there is a way to avoid using these anchor tag, it is fine too. 如果有避免使用这些锚标记的方法,也可以。 I think jQuery has a way to jump to specific selector, right? 我认为jQuery有一种跳转到特定选择器的方法,对吗? Let me know. 让我知道。

Thanks 谢谢

The jQuery ScrollTo plugin could solve your problem. jQuery ScrollTo插件可以解决您的问题。

jQuery.ScrollTo jQuery.ScrollTo

Related: JQuery focus 相关: JQuery重点

Or you could add this script: 或者,您可以添加以下脚本:

clone.find("a[href^=#]").each(function() {
    var anchor = $(this);
    var name = anchor.attr("href");
    anchor.attr("href", name + "_1");
    clone.find("a[name=" + name.substring(1) + "]").attr("name", name.substring(1) + "_1");
});

在创建克隆的同一函数中,还要从原始LI元素中删除name属性。

You can dynamically change the name attribute of the cloned elements: 您可以动态更改克隆元素的名称属性:

$(function() {
    names = ['A', 'B', 'C'];

    $.each(names, function(i, name) {
        $("[name='" + name + "']")[1].name = name + "2";
    });
});

Then you can jump to "#A2" for example. 然后,您可以跳至“#A2”。

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

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