Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

Jquery alert() works but not addclass() or anything else

I try to add an "active" class to a 'a' tag on a 'div' using jquery.
When I put Alert() instead of addclass(), it finds the target (#active0). But when I insert addclass() or anything else , nothing happens.
I'm new on Jquery (and on Forums :) ) and surely forgot something or don't understand a concept.
Question:
How could I do to see the .active class appears on my html to activate the .active class css properties on the web page?
Thank you all in advance for your help and advice.

You could find code bellow.

     $(document).ready(function(){
    $("document").on("click","#active0", function () {  
        $(this).addclass("active");
        });
   });
.folder{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 130px;
    height:30px;  
}

.folder a{
  display: flex;
    justify-content: center;
    align-items: center;
    text-decoration: none;
    color: rgba(1, 113, 185, 0.8);
    width: 130px;
    border-radius: 20px;
    border:1px solid rgba(1, 113, 185, 0.8);
    background-color: white;
    transition-duration:0.6s;  
}

.folder a.active{
    border:3px solid red !important;
}
<hmtl>
  <body>
      <div id="parent2" class="folder">
        <a id="active2" href="#">test</a>
      </div>
      <div id="parent1" class="folder">
        <a id="active1" href="#">test1</a>
      </div>
      <div id="parent" class="folder">
        <a id="active0" href="#">test2</a>
      </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </body>
</hmtl>
question from:https://stackoverflow.com/questions/65900620/jquery-alert-works-but-not-addclass-or-anything-else

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have 2 error:

  1. addClass not addclass , the litter C is capital. jQuery uses camelCase syntax. read more >
  2. $(document) not $("document") because its not an element inside your page. read more >

$(document).ready(function(){
    $(document).on("click","#active0", function () {  
        $(this).addClass("active");
    });
});
.folder{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 130px;
    height:30px;  
}

.folder a{
    display: flex;
    justify-content: center;
    align-items: center;
    text-decoration: none;
    color: rgba(1, 113, 185, 0.8);
    width: 130px;
    border-radius: 20px;
    border:1px solid rgba(1, 113, 185, 0.8);
    background-color: white;
    transition-duration:0.6s;  
}

.folder a.active{
    border:3px solid red !important;
}
<hmtl>
  <body>
      <div id="parent2" class="folder">
        <a id="active2" href="#">test</a>
      </div>
      <div id="parent1" class="folder">
        <a id="active1" href="#">test1</a>
      </div>
      <div id="parent" class="folder">
        <a id="active0" href="#">test2</a>
      </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </body>
</hmtl>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...