2013-12-11 70 views
1

嗨,我試圖讓我的標籤工作通過使用JavaScript,但面臨一些問題,在改變包含內容的股利。在javascript中獲取URL鏈接名稱?

這些都是我的標籤,還有更多,但我會表現出2例如

<li id="sub1" class="moduleTabs"><a href="#">Practice</a> 
     <ul class="collapse"> 
      <li class="unselected"><a href="#pro1" id="t1">Principle</a></li> 
      <li class="unselected"><a href="#pro2" id="t2">Basics</a></li> 
     </ul> 
</li> 

這裏是我的DIV包含內容

<div id="pro1" class="information"> 
    <h2>Principles</h2> 
    <p></p> 
</div> 
<div id="pro2" class="information"> 
    <h2>Basics</h2> 
    <p></p> 
</div> 

這裏是我的javascript代碼

$('#t1, #t2, #t3, #t4, #t5, #t6, #t7, #t8, #t9, #t10, #t11, #t12, #t13, #t14').parent('li').click(function() { 
    $(this).addClass('clicked'); 
    $(this).siblings().removeClass('clicked'); 
    //this part is to remove the content in homepage 
    $('#content').hide(); 
    //i'm trying to get the url name #pro1 if link #t1 is click so that the div will fade in 
    $(this).find('a').attr('href').fadeIn(); 
    $(this).siblings().find('a').attr('href').hide(); 
})/ 

現在我面臨的問題是$(this).find('a').attr('href').fadeIn()不起作用。 我無法從選中的選項卡中獲取網址鏈接(#pro1)

回答

1

問題是,$(this).find('a').attr('href')返回給你一個像「#pro1」,「#pro2」等字符串,而不是直接的jQuery對象。您必須將此字符串賦予jQuery函數以獲取相應的元素。

更改此:

$(this).find('a').attr('href').fadeIn(); 

$($(this).find('a').attr('href')).fadeIn(); 
+0

太感謝你了!有用!! – shiying22

+0

我的榮幸。如果您的問題得到解決,請不要忘記將此答案標記爲已接受。 – OlivierH