最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery ShowHide by class when multiple items contain the said class - Stack Overflow

programmeradmin0浏览0评论

Thanks in advance for helping me out (for those who have time and are willing).

I've written this script:

$(document).ready(function() {
  // hides the slickbox as soon as the DOM is ready
  // (a little sooner than page load)
  $('.foliobtn').hide();
  $('.folionamedate').show();

  // shows the slickbox on clicking the noted link
  $('.foliobottom').mouseover(function() {
    $('.foliobtn').show();
    return false;
  });
  $('.foliobottom').mouseout(function() {
    $('.foliobtn').hide();
    return false;
  });
  $('.foliobottom').mouseover(function() {
    $('.folionamedate').hide();
    return false;
  });
  $('.foliobottom').mouseout(function() {
    $('.folionamedate').show();
    return false;
  });
});

and put it onto this page .php.

It works except it of course shows/hides on every element with the appropriate classes, not just the one I'm hovering over. I can't uniquely name each one as there will be dozens and it will be database driven content.

Does anyone know a simple way to isolate the item I'm hovering over within the script?

Does this make sense?

Thanks in advance for helping me out (for those who have time and are willing).

I've written this script:

$(document).ready(function() {
  // hides the slickbox as soon as the DOM is ready
  // (a little sooner than page load)
  $('.foliobtn').hide();
  $('.folionamedate').show();

  // shows the slickbox on clicking the noted link
  $('.foliobottom').mouseover(function() {
    $('.foliobtn').show();
    return false;
  });
  $('.foliobottom').mouseout(function() {
    $('.foliobtn').hide();
    return false;
  });
  $('.foliobottom').mouseover(function() {
    $('.folionamedate').hide();
    return false;
  });
  $('.foliobottom').mouseout(function() {
    $('.folionamedate').show();
    return false;
  });
});

and put it onto this page http://www.fraservalley-webdesign.com/portfolio/test.php.

It works except it of course shows/hides on every element with the appropriate classes, not just the one I'm hovering over. I can't uniquely name each one as there will be dozens and it will be database driven content.

Does anyone know a simple way to isolate the item I'm hovering over within the script?

Does this make sense?

Share Improve this question edited Dec 19, 2009 at 0:12 Sander Rijken 21.6k3 gold badges64 silver badges86 bronze badges asked Dec 18, 2009 at 23:52 Ben StewartBen Stewart 1051 gold badge2 silver badges5 bronze badges 1
  • yes, it makes sense, and the answer is: Dont do that. Don't hide by class, if you don't want to hide all elements that use that class. Use a different selector. (this works) – Cheeso Commented Dec 19, 2009 at 0:08
Add a comment  | 

4 Answers 4

Reset to default 8

The variable "this" is bound to the triggering element in the mouseover and mouseout handlers, so you can say something like

$('.foliobtn',this).hide();

to hide the elements with class "foliobtn" inside the triggering element.

You can use another function as a callback, this would effectively act as a toggle of sorts, and make your code simpler.

Give this a shot:

$(document).ready(function() {

  // hides the slickbox as soon as the DOM is ready
  // (a little sooner than page load)
  $('.foliobtn').hide();
  $('.folionamedate').show();

  // shows the slickbox on clicking the noted link
  $('.foliobottom').hover(function() {
    $(this).find('.foliobtn').show();
  }, function() {
    $(this).find('.foliobtn').hide();
  });

});

You also don't need to return false in this case because the browser has no default behavior for this element.

return false is more appropriate for something like click for an <a> or a form submit, but really you'd probably want to use preventDefault() instead.

You should use the jquery bind method:

Something like

$(document).ready(function() {
  // hides the slickbox as soon as the DOM is ready
  // (a little sooner than page load)
  $('.foliobtn').hide();
  $('.folionamedate').show();

  // shows the slickbox on clicking the noted link
  $('.foliobottom').mouseover(function(e) {
     $(this).find('.foliobtn').show(); 
     $(this).find('.folionamedate').hide();
  });
  $('.foliobottom').mouseout(function(e) { 
     $(this).find('.foliobtn').hide(); 
     $(this).find('.folionamedate').show();
  });
});

Here you don't change visibility of all elements based on the class, but find an element using this class, and the current node

Could you try this?

$(document).ready(function() {
    // hides the slickbox as soon as the DOM is ready
    // (a little sooner than page load)

    $('.foliobtn').hide();
    $('.folionamedate').show();

    // shows the slickbox on clicking the noted link
    $('.foliobottom').mouseover(function() { $(this).show(); return false; });
    $('.foliobottom').mouseout(function() { $(this).hide(); return false; });
发布评论

评论列表(0)

  1. 暂无评论