简体   繁体   English

jQuery在点击问题上添加类

[英]JQuery adding class on click issue

I am working on a grid of images that when clicked will show a piece of information relating to that grid. 我正在处理一个图像网格,单击该图像将显示与该网格有关的一条信息。 I've managed to get all of that logic working. 我设法使所有逻辑起作用。 However, I've run into an issue with styling the elements on click and hover. 但是,在单击和悬停时设置元素样式时遇到了一个问题。

When the user hovers over each div I would like to display an orange border which I have working. 当用户将鼠标悬停在每个div上时,我想显示一个我正在使用的橙色边框。

    $('.testimonial-box .col-sm-3').hover(function () {
        $(this).toggleClass('testimonials-border'); //Add orange border on hover
    });

When the user clicks on the div I would like to add the orange border permanently until another div is clicked. 当用户单击div时,我想永久添加橙色边框,直到单击另一个div。 This way they can see which one is active. 这样,他们可以看到哪个处于活动状态。

$('.testimonial-box .col-sm-3').click(function () {
    var testimonial = $(this).attr('id');
    $(this).toggleClass('testimonial-border').siblings().removeClass('testimonial-border');
    switch (testimonial) {
        case "testimonial1":
            $('.client-quote').html('Client 1 Testimonial');
            $('.client-name').html('Client 1');
            break;

I've a fiddle of what I'm trying to achieve. 我正在努力实现自己的目标。 There are two rows. 有两行。 The logic works for each row but it doens't work across rows. 该逻辑适用于每一行,但不适用于各行。 If you select a client from the bottom row it will apply the border. 如果从底行选择一个客户,它将应用边框。 If you then select a client from the top row it will not remove the border from the previous div. 如果您随后从第一行中选择一个客户端,它将不会从上一个div删除边框。

https://jsfiddle.net/javacadabra/avbn0myh/ https://jsfiddle.net/javacadabra/avbn0myh/

I'd appreciate any help on this, I can post the code on this as well if you'd like, however it's all containined and working as is in my fiddle above. 非常感谢您的帮助,如果您愿意,我也可以在上面发布代码,但是正如我上面的提琴一样,所有代码都包含在内并且可以正常工作。

Many thanks 非常感谢

You are just removing the class from siblings that is the problem 您只是从兄弟姐妹中删除问题所在的类

 var testimonials = { testimonial1: { name: 'Client 1', testimonial: 'Client 1 Testimonial' }, testimonial2: { name: 'Client 2', testimonial: 'Client 2 Testimonial' }, testimonial3: { name: 'Client 3', testimonial: 'Client 3 Testimonial' }, testimonial4: { name: 'Client 4', testimonial: 'Client 4 Testimonial' }, testimonial5: { name: 'Client 5', testimonial: 'Client 5 Testimonial' }, testimonial6: { name: 'Client 6', testimonial: 'Client 6 Testimonial' }, testimonial7: { name: 'Client 7', testimonial: 'Client 7 Testimonial' }, testimonial8: { name: 'Client 8', testimonial: 'Client 8 Testimonial' } } $('.testimonial-box').on('mouseenter mouseleave', '.col-sm-3:not(.testimonial-border)', function(e) { $(this).toggleClass('testimonials-border', e.type == 'mouseenter'); //Add orange border on hover }); $('.testimonial-box .col-sm-3').click(function() { $(this).addClass('testimonial-border').removeClass('testimonials-border').closest('.testimonial-box').find('.testimonial-border').not(this).removeClass('testimonial-border'); var testimonial = testimonials[this.id] || {}; $('.client-quote').html(testimonial.testimonial || ''); $('.client-name').html(testimonial.name || ''); }); 
 .helper { display: inline-block; height: 100%; vertical-align: middle; } .testimonial-border { border: 1px solid #f39300; } .testimonials-border { border: 1px solid #f39300; } .col-md-7.col-md-offset-1.testimonial-box { background: #fff; padding: 0; border-radius: 4px; } .testimonial-box .row { margin: 0; } .row.client-info { background: white; padding: 5%; min-height: 285px; } .testimonial-box h3 { border-top-left-radius: 4px; border-top-right-radius: 4px; } .client-info .fa-quote-left { float: left; color: #f39300; } .client-info .fa-quote-right { float: right; color: #f39300; } p.client-name { font-weight: 400; } 
 <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css" /> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.css" /> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.js"></script> <div class="col-md-7 col-md-offset-1 testimonial-box"> <div class="row"> <div class="col-sm-3" id="testimonial1"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial2"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial3"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial4"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> </div> <div class="row"> <div class="col-sm-3" id="testimonial5"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial6"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial7"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial8"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> </div> <div class="row client-info"> <div class="col-md-12"> <i class="fa fa-quote-left"></i> <p class="client-quote">Client 1 Testimonial</p> <i class="fa fa-quote-right"></i> <p class="client-name">Client 1</p> </div> </div> </div> 

Try this: 尝试这个:

var testimonialBoxes = $('.testimonial-box .col-sm-3');
var lastSelected = $( testimonialBoxes[0] );  // initialize with first element

testimonialBoxes.click(function () {

    var testimonial = $(this).attr('id');
    var $this = $( this );

    lastSelected.removeClass('testimonial-border');
    $this.addClass('testimonial-border');
    lastSelected = $this;

    .....................

you can try this code : 您可以尝试以下代码:

$('.testimonial-box .col-sm-3').click(function(){
     $('.testimonial-box .col-sm-3').each(function () {
            $(this).removeClass("testimonial-border");
        });
  $(this).addClass("testimonial-border");
});

I did not test the code but it should work fine 我没有测试代码,但应该可以正常工作

i see if you select the tag with the testimonial-border class and then just remove that class, it works... 我看到您是否选择了带有见证边框类的标签,然后仅删除该类,它就可以工作...

$('.testimonial-box .col-sm-3').click(function () {
var testimonial = $(this).attr('id');
$(".testimonial-border").removeClass("testimonial-border");
$(this).toggleClass('testimonial-border');
switch (testimonial) {
    case "testimonial1":
        $('.client-quote').html('Client 1 Testimonial');
        $('.client-name').html('Client 1');
        break;
    case "testimonial2":
        $('.client-quote').html('Client 2 Testimonial');
        $('.client-name').html('Client 2');
        break;
    case "testimonial3":
        $('.client-quote').html('Client 3 Testimonial');
        $('.client-name').html('Client 3');
        break;
    case "testimonial4":
        $('.client-quote').html('Client 4 Testimonial');
        $('.client-name').html('Client 4');
        break;
    case "testimonial5":
        $('.client-quote').html('Client 5 Testimonial');
        $('.client-name').html('Client 5');
        break;
    case "testimonial6":
        $('.client-quote').html('Client 6 Testimonial');
        $('.client-name').html('Client 6');
        break;
    case "testimonial7":
        $('.client-quote').html('Client 7 Testimonial');
        $('.client-name').html('Client 7');
        break;
    case "testimonial8":
        $('.client-quote').html('Client 8 Testimonial');
        $('.client-name').html('Client 8');
        break;
}

}); });

$('.testimonial-box .col-sm-3').click(function () {
$('.testimonial-box .col-sm-3').removeClass('testimonial-border');
var testimonial = $(this).attr('id');
$(this).toggleClass('testimonial-border');

switch (testimonial) {.....

Change .click() function and add the above code and it works.. 更改.click()函数并添加上面的代码,它即可工作。

 $('.testimonial-box .col-sm-3').hover(function () { $(this).toggleClass('testimonials-border'); //Add orange border on hover }); $('.testimonial-box .col-sm-3').click(function () { $('.testimonial-box .col-sm-3').removeClass('testimonial-border'); //added var testimonial = $(this).attr('id'); $(this).toggleClass('testimonial-border'); //changed switch (testimonial) { case "testimonial1": $('.client-quote').html('Client 1 Testimonial'); $('.client-name').html('Client 1'); break; case "testimonial2": $('.client-quote').html('Client 2 Testimonial'); $('.client-name').html('Client 2'); break; case "testimonial3": $('.client-quote').html('Client 3 Testimonial'); $('.client-name').html('Client 3'); break; case "testimonial4": $('.client-quote').html('Client 4 Testimonial'); $('.client-name').html('Client 4'); break; case "testimonial5": $('.client-quote').html('Client 5 Testimonial'); $('.client-name').html('Client 5'); break; case "testimonial6": $('.client-quote').html('Client 6 Testimonial'); $('.client-name').html('Client 6'); break; case "testimonial7": $('.client-quote').html('Client 7 Testimonial'); $('.client-name').html('Client 7'); break; case "testimonial8": $('.client-quote').html('Client 8 Testimonial'); $('.client-name').html('Client 8'); break; } }); 
 .helper { display: inline-block; height: 100%; vertical-align: middle; } .testimonial-border { border:1px solid #f39300; } .testimonials-border { border:1px solid #f39300; } .col-md-7.col-md-offset-1.testimonial-box { background: #fff; padding: 0; border-radius: 4px; } .testimonial-box .row { margin: 0; } .row.client-info { background: white; padding: 5%; min-height:285px; } .testimonial-box h3 { border-top-left-radius: 4px; border-top-right-radius: 4px; } .client-info .fa-quote-left { float:left; color: #f39300; } .client-info .fa-quote-right { float:right; color: #f39300; } p.client-name { font-weight: 400; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="col-md-7 col-md-offset-1 testimonial-box"> <div class="row"> <div class="col-sm-3" id="testimonial1"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial2"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial3"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial4"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> </div> <div class="row"> <div class="col-sm-3" id="testimonial5"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial6"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial7"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> <div class="col-sm-3" id="testimonial8"> <span class="helper"></span> <img src="https://wiki.maemo.org/images/thumb/d/de/Maemo.org_logo_contest_sample1_bundyo.png/300px-Maemo.org_logo_contest_sample1_bundyo.png" width="80"> </div> </div> <div class="row client-info"> <div class="col-md-12"> <i class="fa fa-quote-left"></i> <p class="client-quote">Client 1 Testimonial</p> <i class="fa fa-quote-right"></i> <p class="client-name">Client 1</p> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM