开发者

what's wrong with my jquery hasClass?

开发者 https://www.devze.com 2023-02-14 06:59 出处:网络
<!DOCTYPE HTML> <html> <head> <meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" />
<!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开发者_StackOverflow").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>

For some reason i can't find the answer on stack OR google. Maybe i haven't searched enough. But i keep getting: false istead of true no matter what i do. Yet, the class current is asigned to tab1.

Thx in advanced


$("#pics_tab1") is an id selector, not a href selector. Change your anchor tags' href attributes to id attributes, and remove the #:

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


You are confusing your href with your id -

<!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>


You've put the id code inside the href property. You need to use id="pics_tab1"


The id property was absent.

try

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


Well, you need to have the id attribute of the element set to pics_tab1, not the href.


in jQuery, ("#pics_tab1") selects "elements with id = pics_tab1. You have no elements on your page with this id.

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

should be

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


You'll need to use the attribute equals selector:

$('a[href="#pics_tab1"]').hasClass("current");
0

精彩评论

暂无评论...
验证码 换一张
取 消