2009-04-28 43 views
0

我試過找到一個simialr的例子,並用它來回答我的問題,但我似乎無法得到它的工作,所以道歉,如果這聽起來類似於其他問題。添加一個錨到生成的URL

基本上,我使用終端四的Site Manager CMS系統來構建我的網站。此工具可讓您生成導航元素,以便在整個網站中使用。

我需要添加一個JS的自定義位以追加到這些鏈接一個錨點。

生成的鏈接類似於此:

<ul id="tab-menu"> 
<li><a href="/section/page">test link, can i rewrite and add an anchor!!!</a></li> 
</ul> 

我可以編輯鏈接的CSS屬性,但我無法弄清楚如何添加錨點。

我使用jQuery如下:提前任何幫助

<script type="text/javascript" src="http://jquery.com/src/jquery-latest.pack.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
    // everything goes here 

    $("#tab-menu").children("li").each(function() { 
     $(this).children("a").css({color:"red"}); 

     }); 

    }); 
</script> 

感謝。

水稻

+0

好問題,但一個人如何做沒有jQuery的? – JohnK 2012-10-02 15:57:07

回答

1

一個不錯的基於jQuery的方法是使用獲得(index)方法到您的每個()函數內訪問原始DOM元素。然後,您可以訪問JavaScript鏈接對象,該對象具有名爲「hash」的屬性,該屬性表示url的錨點部分。因此,修改你的代碼稍微:

<script type="text/javascript"> 
    $(document).ready(function(){ 
    // everything goes here 

     $("#tab-menu").children("li").children("a").each(function() { 
      $(this).css({color:"red"}).get(0).hash = "boom"; 
     }); 

    }); 

會在「#tab_menu禮」的所有鏈接變爲紅色,而「#boom」附加到年底。

希望這會有所幫助!

+0

謝謝,這個作品完美。 稻田 – 2009-04-28 12:30:44

0

我現在可以通過使用針對HTML如下:

$(this).children("a").html("it works"); 

我作爲因爲:

$(this).children("a").href("something"); 

會編輯href,但我錯了。

水稻

+0

使用類似$(this).children(「a」)。attr(「href」,「something」)的東西,你很好。 – moff 2009-04-28 08:54:02

0

我不知道答案,我不力嘗試

$("#tab-menu").children("li").children("a").each(function() { 
       // $(this).css({color:"red"}).get(0).hash = "boom"; 
      var temp_url = $(this).href +'#an_anchor';//or var temp_url = $(this).attr('href'); 
      $(this).attr('href', temp_url); 
    }); 
相關問題