te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Append #hashtag to link in HTML on the fly - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Append #hashtag to link in HTML on the fly - Stack Overflow

programmeradmin3浏览0评论

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:

.html#work

I have a series of links in the page:

<div id="links">
<ul>
  <li><a href="">Link1</a></li>
  <li><a href="">Link2</a></li>
  <li><a href="">Link3</a></li>
  <li><a href="">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?

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./page.html#work

I have a series of links in the page:

<div id="links">
<ul>
  <li><a href="http://www.domain./link1">Link1</a></li>
  <li><a href="http://www.domain./link2">Link2</a></li>
  <li><a href="http://www.domain./link3">Link3</a></li>
  <li><a href="http://www.domain./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?

Share Improve this question edited Nov 30, 2011 at 22:47 Lightness Races in Orbit 385k77 gold badges665 silver badges1.1k bronze badges asked Oct 7, 2010 at 19:01 Robert ERobert E 7404 gold badges17 silver badges29 bronze badges 9
  • 1 I think we all answered with differing variations on how we read your question. I thought you wanted to append the link text as the hash (e.g. www.#link1), Chigley thinks you want to append #work to them all (valid interpretation), and I'm not sure what Adam thinks (He just posted a few seconds ago, so I haven't read it). Maybe a slight clarification would help us out. – jps Commented Oct 7, 2010 at 19:11
  • 1 @Jud - would have +1'd your ment if it wasn't for the misspelling of chigley :P (You're not the first, and certainly won't be the last! Don't get why it happens so often...) – chigley Commented Oct 7, 2010 at 19:13
  • @chigley - haha, thats what I get for always scanning everything instead of really "reading" it. Gets me in trouble all the time. – jps Commented Oct 7, 2010 at 19:15
  • @Jud- I assumed the same as chigley, with the slight the difference that my code takes the hashtag from the url the user is currently at. – Adam Commented Oct 7, 2010 at 19:20
  • ha, and bobince gets the upvote. ;) – jps Commented Oct 7, 2010 at 19:26
 |  Show 4 more ments

8 Answers 8

Reset to default 4

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;
});​​​​​​​

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>

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)
  });

Try this:

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

You should use livequery plugin : http://plugins.jquery./project/livequery and the documentation is here http://brandonaaron/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');
          });
$("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)
    });
   }
  });

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

发布评论

评论列表(0)

  1. 暂无评论