防止点击,更改类名,隐藏亮点 - jQuery

时间:2017-10-23 10:46:45

标签: javascript jquery class button highlight

我在这个应用程序中有三个问题。

第一个问题是,在我们打开应用后关闭应用时,show按钮的名称不会更改为“show offer”。

第二次,该应用不会阻止用户点击第二个和第三个按钮。他只能点击一个优惠,但现在用户可以点击每个优惠。

第三次,用户可以通过点击一次突出显示新的优惠,然后他可以隐藏突出显示,但不能再次突出显示它,从不。感谢任何帮助。

//// hide offer before the Dom is loaded
//$('ul').hide()
$(document).ready(function() {   
    
    function showHideOffer() {
        $('ul').slideToggle();
    }
    
    //click to show offers
    $('.card').on('click', '.showOffers', showHideOffer, function() {
       $('.showOffers').html('Hide Offers');
    });
    
    //click to hide offers - change name to show offers doesn't work!!
    $('.card').on('click','.showOffers', showHideOfferfunction() {
   $('.showOffers').html('Show Offers');
});
    
    // click to book, to show info and close button and span
   $('li').on('click', 'button', function(){
      var offerName = $(this).closest('.tour').data('name');
      var offerPrice = $(this).closest('.tour').data('price')
      var message = $('<ol class="breadcrumb"><li class="breadcrumb-item active" style="color:#3CB371">Success! You have booked '+offerName+' offer for '+offerPrice+'!</li></ol>');
       $(this).closest('.tour').append(message);
       
       $(this).prev().closest('.details').remove();
       $(this).remove();     
   });
    
    // filter new offers by clicking a "new" button
    $('#buttons').on('click','.filterNew', function() {
            $('.tour').filter('.newOffer').addClass('highlightnew');
            $('.highlightpopular').removeClass('highlightpopular');
               //click second time and the highlight is hidden
               $('#buttons').on('click', '.filterNew', function() {
                    $('.highlightnew').removeClass('highlightnew');
                });
        });
    
        // filter by popular offers
        $('#buttons').on('click', '.filterPopular', function() { 
            $('.tour').filter('.popular').addClass('highlightpopular');
            $('.highlightnew').removeClass('highlightnew');
        });
    
    
});
<!DOCTYPE html>
<html>
    <head>

        <meta charset="utf-8">
        <title>GuidedTours</title>
        <link rel="stylesheet" type="text/css" href="wageup.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <style>
          .selected {
            color: red;
          }
          .highlightnew {
            background: #3D9970;
          }
            .highlightpopular {
                background: #39CCCC;
            }
            ul {
                display:none;
            }
      </style>
    </head>
    <body>
    <div class="container">
       <h2>Guided Tours</h2>
            <hr>
            <div id="tours" class="card">
                <!-- click to show -->
                <button type="button" value="Show Offers" class="btn showOffers btn-primary btn-sm">Show Offers</button>
                
              <ul class="list-group list-group-flush">
                <li class="usa tour newOffer list-group-item"; data-name="New York" data-price="$550">
                  <h3>New York, New York</h3>
                  <span class="details badge badge-success">$1,899 for 7 nights</span>
                  <button class="book btn btn-primary">Book Now</button>
                </li>

                <li class="europe tour newOffer list-group-item" data-name="Paris" data-price="$450">
                  <h3>Paris, France</h3>
                  <span class="details badge badge-success">$2,299 for 7 nights</span>
                  <button class="book btn btn-primary">Book Now</button>
                </li>

                <li class="asia tour popular list-group-item" data-name="Tokyo" data-price="$650">
                  <h3>Tokyo, Japan</h3>
                  <span class="details badge badge-success">$3,799 for 7 nights</span>
                  <button class="book btn btn-primary">Book Now</button>
                </li>
              </ul>
                <ul id="buttons" class="list-group list list-group-flush">
                <button class="filterNew btn btn-info">New</button>
                <button class="filterPopular btn btn-info">Popular</button>
                </ul>
            </div>
        </div>





        <!-- Scripts -->

        <!-- Jquery -->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
        <!-- Bootstrap -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <!-- JS -->
        <script type="text/javascript" src="guidedtours.js"></script>

    </body>
</html>

2 个答案:

答案 0 :(得分:0)

第一期解决:在此结帐: 将代码更改为

$('.card').on('click', '.showOffers', showHideOffer, function() {
       //$('.showOffers').html('');

       $(this).text( $(this).text() == 'Hide Offers' ? "Show Offers" : "Hide Offers");
    });

代替:

$('.card').on('click', '.showOffers', showHideOffer, function() {
       $('.showOffers').html('Hide Offers');
    });

&#13;
&#13;
    
    
    //click to show offers
    $('.card').on('click', '.showOffers', showHideOffer, function() {
       //$('.showOffers').html('');
       
       $(this).text( $(this).text() == 'Hide Offers' ? "Show Offers" : "Hide Offers");
    });
    
    //click to hide offers - change name to show offers doesn't work!!
   $('.card').on('click','.showOffers', showHideOffer);
    
    // click to book, to show info and close button and span
   $('li').on('click', 'button', function(){
      var offerName = $(this).closest('.tour').data('name');
      var offerPrice = $(this).closest('.tour').data('price')
      var message = $('<ol class="breadcrumb"><li class="breadcrumb-item active" style="color:#3CB371">Success! You have booked '+offerName+' offer for '+offerPrice+'!</li></ol>');
       $(this).closest('.tour').append(message);
       
       $(this).prev().closest('.details').remove();
       $(this).remove();     
   });
    
    // filter new offers by clicking a "new" button
    $('#buttons').on('click','.filterNew', function() {
            $('.tour').filter('.newOffer').addClass('highlightnew');
            $('.highlightpopular').removeClass('highlightpopular');
               //click second time and the highlight is hidden
               $('#buttons').on('click', '.filterNew', function() {
                    $('.highlightnew').removeClass('highlightnew');
                });
        });
    
        // filter by popular offers
        $('#buttons').on('click', '.filterPopular', function() { 
            $('.tour').filter('.popular').addClass('highlightpopular');
            $('.highlightnew').removeClass('highlightnew');
        });
    
    
function showHideOffer() {
        $('ul').slideToggle();
    }
&#13;
   .selected {
            color: red;
          }
          .highlightnew {
            background: #3D9970;
          }
            .highlightpopular {
                background: #39CCCC;
            }
            ul {
                display:none;
            }
&#13;
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>



    
    <div class="container">
       <h2>Guided Tours</h2>
            <hr>
            <div id="tours" class="card">
                <!-- click to show -->
                <button type="button" value="Show Offers" class="btn showOffers btn-primary btn-sm">Show Offers</button>
                
              <ul class="list-group list-group-flush">
                <li class="usa tour newOffer list-group-item"; data-name="New York" data-price="$550">
                  <h3>New York, New York</h3>
                  <span class="details badge badge-success">$1,899 for 7 nights</span>
                  <button class="book btn btn-primary">Book Now</button>
                </li>

                <li class="europe tour newOffer list-group-item" data-name="Paris" data-price="$450">
                  <h3>Paris, France</h3>
                  <span class="details badge badge-success">$2,299 for 7 nights</span>
                  <button class="book btn btn-primary">Book Now</button>
                </li>

                <li class="asia tour popular list-group-item" data-name="Tokyo" data-price="$650">
                  <h3>Tokyo, Japan</h3>
                  <span class="details badge badge-success">$3,799 for 7 nights</span>
                  <button class="book btn btn-primary">Book Now</button>
                </li>
              </ul>
                <ul id="buttons" class="list-group list list-group-flush">
                <button class="filterNew btn btn-info">New</button>
                <button class="filterPopular btn btn-info">Popular</button>
                </ul>
            </div>
        </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

To Resolve your first issue modify your function like below 
var ValuesShowOrHidden = 0;
$('.card').on('click', '.showOffers', showHideOffer, function () {
                if (ValuesShowOrHidden == 0) {
                    $('.showOffers').html('Hide Offers');
                    ValuesShowOrHidden = 1;

                }
                else if (ValuesShowOrHidden==1) {
                    $('.showOffers').html('Show Offers');
                    ValuesShowOrHidden = 0;
                }
            });

对于第二期,请在函数中尝试以下修改

$('li').on('click', 'button', function () {


                var offerName = $(this).closest('.tour').data('name');
                var offerPrice = $(this).closest('.tour').data('price')
                var message = $('<ol class="breadcrumb"><li class="breadcrumb-item active" style="color:#3CB371">Success! You have booked ' + offerName + ' offer for ' + offerPrice + '!</li></ol>');
                $(this).closest('.tour').append(message);

                $(this).prev().closest('.details').remove();
                $(this).remove();
                $('li').unbind("click");
            });