将 #hashtag 动态附加到 HTML 中的链接

发布于 2024-09-26 05:34:19 字数 729 浏览 0 评论 0原文

我有一个导航,导航中的每个链接只是在单击时在现有 URL 上附加一个主题标签,以便使用 jQuery 进行实时过滤等。

我想将相同的当前主题标签附加到页面下方 div 内的一系列链接。

例如,我单击了“work”,我的 URL 现在如下所示:

http://www.domain.com/page.html#work

我的页面中有一系列链接:

<div id="links">
<ul>
  <li><a href="http://www.domain.com/link1">Link1</a></li>
  <li><a href="http://www.domain.com/link2">Link2</a></li>
  <li><a href="http://www.domain.com/link3">Link3</a></li>
  <li><a href="http://www.domain.com/link4">Link4</a></li>
</ul>
</div>

div#links 中的这些链接需要动态更新以在所有 URL 上附加 #work,因此单击时会附加主题标签。

这可能吗?这有道理吗?

I have a nav, each link in the nav just appends a hashtag on the existing URL when clicked, for live filtering, etc. with the use of jQuery.

I want to append the SAME current hashtag to a series of links within a div further down the page.

For example, I've clicked "work" and my URL now looks like:

http://www.domain.com/page.html#work

I have a series of links in the page:

<div id="links">
<ul>
  <li><a href="http://www.domain.com/link1">Link1</a></li>
  <li><a href="http://www.domain.com/link2">Link2</a></li>
  <li><a href="http://www.domain.com/link3">Link3</a></li>
  <li><a href="http://www.domain.com/link4">Link4</a></li>
</ul>
</div>

These links within the div#links need to be updated on the fly to append #work on all the URL's so that when clicked the hashtag is appended.

Is this possible? Does this make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

Smile简单爱 2024-10-03 05:34:19

使用链接的 hash 属性来设置片段标识符,而不会弄乱 href 的其余部分:

$('#links a').each(function() {
    this.hash= location.hash;
});​​​​​​​

Use the hash property of links to set the fragment identifier without messing around with the rest of the href:

$('#links a').each(function() {
    this.hash= location.hash;
});​​​​​​​
寄离 2024-10-03 05:34:19

您应该为 #nav 中的链接附加一个 click 事件处理程序,并相应地更改 #links 中的链接。 [查看实际操作]

Javascript

$("#nav a").click(function() {
  $("#links a").each(function() {
    this.href = this.href.split("#")[0] + "#" + window.location.hash;
  });
});​

HTML

<div id="nav">  
  <a href="#work">work</a> - 
  <a href="#school">school</a>
</div>

You should attach a click event handler for links in #nav and change links in #links accordingly. [See it in action]

Javascript

$("#nav a").click(function() {
  $("#links a").each(function() {
    this.href = this.href.split("#")[0] + "#" + window.location.hash;
  });
});​

HTML

<div id="nav">  
  <a href="#work">work</a> - 
  <a href="#school">school</a>
</div>
拧巴小姐 2024-10-03 05:34:19

下面的 jQuery 代码将选择 each

  • $("div#links li a").each(
      function(index, element){
         $(this).attr('href',$(this).attr('href') + window.location.hash)
      });
    

    The jQuery code below will select each <a> within <li> within the <div> with the id of links and change its href attribute to be the same as its current value but with the hash of the current page appended to it.

    $("div#links li a").each(
      function(index, element){
         $(this).attr('href',$(this).attr('href') + window.location.hash)
      });
    
    猛虎独行 2024-10-03 05:34:19

    试试这个:

    $(function() {
        $('#links a').each(function() {
            $(this).attr('href', $(this).attr('href')+'#work');
        });​​​​​​​
    });
    

    Try this:

    $(function() {
        $('#links a').each(function() {
            $(this).attr('href', $(this).attr('href')+'#work');
        });​​​​​​​
    });
    
    无所的.畏惧 2024-10-03 05:34:19
    $('#links li a').each(function()
    {
        $(this).attr('href', '#' + $(this).html());
    });
    
    $('#links li a').each(function()
    {
        $(this).attr('href', '#' + $(this).html());
    });
    
    我很坚强 2024-10-03 05:34:19

    您应该使用 livequery 插件: http://plugins.jquery.com/project/livequery 和文档在这里 http://brandonaaron.net/code/livequery/docs

    编辑:
    Live Query 通过绑定事件或自动触发匹配元素的回调来利用 jQuery 选择器的强大功能,即使在页面加载且 DOM 更新之后也是如此

     $("#links a").livequery('click',function(event) {
                     $(this).attr('href', $(this).attr('href')+'#work');
              });
    

    You should use livequery plugin : http://plugins.jquery.com/project/livequery and the documentation is here http://brandonaaron.net/code/livequery/docs

    edit:
    Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated

     $("#links a").livequery('click',function(event) {
                     $(this).attr('href', $(this).attr('href')+'#work');
              });
    
    深居我梦 2024-10-03 05:34:19
    $("a[href]").click(function(){
       var href = $(this).attr('href');
       var ind = href.indexOf('#');
       if ( ind != -1 ) {
        var hash = href.substring(ind+1);
        $("div#links li a").each(function() {
         var myHref = $(this).attr('href');
         var myInd = myHref('#');
         if ( myInd != -1 ) {
          myHref = myHref.substring(0, myInd);
         }
         $(this).attr('href', myHref + hash)
        });
       }
      });
    
    $("a[href]").click(function(){
       var href = $(this).attr('href');
       var ind = href.indexOf('#');
       if ( ind != -1 ) {
        var hash = href.substring(ind+1);
        $("div#links li a").each(function() {
         var myHref = $(this).attr('href');
         var myInd = myHref('#');
         if ( myInd != -1 ) {
          myHref = myHref.substring(0, myInd);
         }
         $(this).attr('href', myHref + hash)
        });
       }
      });
    
    很酷又爱笑 2024-10-03 05:34:19

    请查看此页面,了解带有 hasgtag、ajax 和 html 历史 API 的完整演示 http://www.amitpatil.me/create-gmail-like-app-using-html5-history-api-and-hashbang/

    Take a look at this page for complete demo with hasgtag,ajax and html history API http://www.amitpatil.me/create-gmail-like-app-using-html5-history-api-and-hashbang/

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文