2011-03-01 62 views
0
<!DOCTYPE HTML> 
<html> 
<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<title>abc</title> 
<script type="text/javascript" src="scripts/jquery_latest.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
if($("#pics_tab1").hasClass("current")) 
    alert("tab1");  
else 
    alert("false"); 
}); 
</script> 
</head> 
<body> 
<div class="tabs"> 
     <a href="#pics_tab1" class="current"></a> 
     <a href="#pics_tab2"></a> 
     <a href="#pics_tab3"></a> 
     <a href="#pics_tab4"></a> 
     <a href="#pics_tab5"></a> 
    </div> 

我的jquery hasClass有什麼問題?

出於某種原因,我無法找到棧或谷歌的答案。也許我沒有足夠的搜索。但是我總是得到:無論我做什麼,都是虛假的。然而,班級電流與tab1一致。

Thx in advanced

回答

3

您與您的id混淆你的href -

<!DOCTYPE HTML> 
<html> 
<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<title>abc</title> 
<script type="text/javascript" src="scripts/jquery_latest.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() 
{ 
if($("#pics_tab_link1").hasClass("current")) 
    alert("tab1");  
else 
    alert("false"); 
}); 
</script> 
</head> 
<body> 

<div class="tabs"> 
    <a id="pics_tab_link1" href="#pics_tab1" class="current"></a> 
    <a href="#pics_tab2"></a> 
    <a href="#pics_tab3"></a> 
    <a href="#pics_tab4"></a> 
    <a href="#pics_tab5"></a> 
</div> 
+0

非常感謝thx。我寧願離開這些hrefs,只是ad id的同名。只知道讓我檢查.. – PathOfNeo 2011-03-01 13:45:48

2

您已將id代碼放入href屬性中。您需要使用id =「pics_tab1」

+0

這還是不行,你不會想把#裏面的id屬性。 – 2011-03-01 13:42:28

+0

耶認識到和編輯 – 2011-03-01 13:42:57

4

$("#pics_tab1")是一個id選擇器,而不是href選擇器。改變你的錨標記HREF屬性的id屬性,並刪除#

<a id="pics_tab1" href="yourlinkhere"></a> 
+0

o沒有看到o我的...如果有可能我不會更改hrefs到id的 - >那裏的一部分api(我使用:touchscroll模仿iphone滾動行爲Web應用程序)。 – PathOfNeo 2011-03-01 13:44:54

+0

如果需要保留hrefs,可以使用屬性equals選擇器。請參閱下面的答案。 – StuperUser 2011-03-01 13:49:48

1

id屬性是不存在的。

嘗試

<a id="pics_tab1" href="#pics_tab1" class="current"></a> 
1

那麼,你需要有設置爲pics_tab1,而不是href元素的id屬性。

1
jQuery中

,( 「#pics_tab1」),選擇「使用id = pics_tab1元素。 你有你的頁面上的任何元素,與此ID。

<a href="#pics_tab1" class="current"></a> 

應該

<a id="pics_tab1" href="#pics_tab1" class="current"></a>