开发者

jQuery Adding a css class to a link when clicked

开发者 https://www.devze.com 2023-02-21 20:50 出处:网络
I have the current code: <script type=\"text/javascript\"> $(\'ul.products-slideshow-thumbs li a\').bind(\"mousedown\", function(){

I have the current code:

<script type="text/javascript">
$('ul.products-slideshow-thumbs li a').bind("mousedown", function(){
  $(this).toggleClass("current");
});
</script>

<ul class="products-slideshow-thumbs">
  <li><a href="#"><img src="product-slides/thumbs/1.jpg" /></a></li>
  <li><开发者_如何学JAVA;a href="#"><img src="product-slides/thumbs/2.jpg" /></a></li>
  <li><a href="#"><img src="product-slides/thumbs/3.jpg" /></a></li>
</ul>

But it's not working properly (basically, nothing happens).

Any suggestions? (I'm afraid I'm fairly new to jQuery/JavaScript.) Thank you!


Try this:

<script type="text/javascript">
 $(document).ready(function(){   
   $('ul.products-slideshow-thumbs li a').click(function(){
      $(this).toggleClass("current");
    });
  });
</script>

<ul class="products-slideshow-thumbs">
  <li><a href="#"><img src="product-slides/thumbs/1.jpg" /></a></li>
  <li><a href="#"><img src="product-slides/thumbs/2.jpg" /></a></li>
  <li><a href="#"><img src="product-slides/thumbs/3.jpg" /></a></li>
</ul>


I copied your code and it seemed to work for me. Have you also tried the .click() jquery method?

http://jsfiddle.net/jordanlewis/X6mn5/

0

精彩评论

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