简体   繁体   English

如何在jQuery中将类添加到所选范围?

[英]How to add a class to the selected span in jQuery?

I'm trying to set a class (highlighted) to the span been selected and removing it when another span has been selected. 我正在尝试为选中的范围设置一个类(突出显示),并在选择另一个范围时将其删除。 Where do I need to set this class? 我需要在哪里设置此类?

This is for a king of telephone keypad I'm working on. 这是我正在使用的电话键盘之王。 I'm basically working on a code that someone just answered on a previous question I posted. 我基本上是在编写代码,有人刚刚回答了我之前发布的问题。 I've been trying to set this class, but I can't make it work as I wish. 我一直在尝试设置此类,但是我无法使其按预期工作。

Here is the code: http://jsfiddle.net/b6wfeaxz/3/ 这是代码: http : //jsfiddle.net/b6wfeaxz/3/

JS / jQuery JS / jQuery

var index     = 0;
var direction = 1;

$(".button").on("click", function () {

  var $this = $(this);

  if ($this.hasClass("selected")) {
    if (index >= $this.find(".letter").length - 1) {
        direction *= -1;
    } else if (direction == -1 && index == 0) {
        direction *= -1;
    }

        $(this).children(".letter").eq(index).addClass('highlighted');
    index += direction;
  } else {
    $(".button").removeClass("selected");
    $this.addClass("selected");

    index = 0;
  }

  var result = $(this).children(".letter").eq(index).text();

  $(".result").text(result);

});

HTML - HTML-

<div class="keypad">
  <div class="button">
    <div class="num">1</div>
    <span class="letter">A</span>
    <span class="letter">B</span>
    <span class="letter">C</span>
  </div>
  <div class="button">
    <div class="num">2</div>
    <span class="letter">D</span>
    <span class="letter">E</span>
    <span class="letter">F</span>
  </div>
  <div class="button">
    <div class="num">3</div>
    <span class="letter">G</span>
    <span class="letter">H</span>
    <span class="letter">I</span>
  </div>
</div>

CSS CSS

 .highlighted {
   border: 1px solid red;
 }

I expect the span to be selected by adding the class highlighted and be removed when another letter has been selected. 我希望通过添加突出显示的类来选择跨度,并在选择另一个字母时将其删除。

From what I could understand the problem is removing the class from the unselected letters, right? 据我了解,问题是从未选中的字母中删除了类,对吗? Try using the each function to remove the class from all letters first. 尝试使用each函数首先从所有字母中删除该类。

Like this: 像这样:

$('.letter').each(function(){
    $(this).removeClass('highlighted');
})
$(this).children(".letter").eq(index).addClass('highlighted');
index += direction;

This is a quick fix. 这是一个快速修复。 Basically you keep an array of all the directions and indexes for each of the buttons. 基本上,您将保留每个按钮的所有方向和索引的数组。 Then you select which one you need to edit by the index of the button clicked. 然后,通过单击按钮的索引选择需要编辑的那个。 This is not an ideal solution but it works. 这不是理想的解决方案,但它可以工作。 Hopefully that helped. 希望能有所帮助。

var index     = [0,0,0];
var direction = [1,1,1];

$(".button").on("click", function () {

  var $this = $(this);
    if (index[$this.index()] >= $this.find(".letter").length - 1) {
        direction[$this.index()] *= -1;
    } else if (direction[$this.index()] == -1 && index[$this.index()] == 0) {           
        direction[$this.index()] *= -1;
    }
    $('.letter').each(function(){
        $(this).removeClass('highlighted');
    })
    $(this).children(".letter").eq(index[$this.index()]).addClass('highlighted');

  var result = $(this).children(".letter").eq(index[$this.index()]).text();
  $(".result").text(result);
  //you have to add to the index after the result
  index[$this.index()] += direction[$this.index()];

});

This is a different approach, but seems easier to separate the highlight logic from the selected click event. 这是一种不同的方法,但似乎更容易将突出显示逻辑与selected单击事件分开。

 var index = 0; var direction = 1; $(".button").on("click", function () { var $this = $(this); if ($this.hasClass("selected")) { if (index >= $this.find(".letter").length - 1) { direction *= -1; } else if (direction == -1 && index == 0) { direction *= -1; } index += direction; } else { $(".button").removeClass("selected"); $this.addClass("selected"); index = 0; } removeHighlight(); $(this).find('span.letters').addClass('highlighted'); var result = $(this).find(".letter").eq(index).text(); $(".result").text(result); }); function removeHighlight() { var $letters = $('div.keypad').find('.letters'); $letters.each(function() { $(this).removeClass('highlighted'); }); } 
 .button { float: left; width: 100px; height: 100px; margin: 20px 20px; } .num { font-size: 50px; } .result { font-size: 100px; color: blue; padding: 50px; } .highlighted { border: 1px solid red; } div.num { cursor: pointer; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="result">A</div> <div class="keypad"> <div class="button"> <div class="num">1</div> <span class="letters"> <span class="letter">A</span> <span class="letter">B</span> <span class="letter">C</span> </span> </div> <div class="button"> <div class="num">2</div> <span class="letters"> <span class="letter">D</span> <span class="letter">E</span> <span class="letter">F</span> </span> </div> <div class="button"> <div class="num">3</div> <span class="letters"> <span class="letter">G</span> <span class="letter">H</span> <span class="letter">I</span> </span> </div> </div> 

Hi I have change your code a bit the changes are : 嗨,我对您的代码做了一些更改,这些更改是:

  1. Add a counter to detect each click. 添加计数器以检测每次点击。
  2. Add a num variable and actual_number in order to reset the counter between buttons. 添加一个num变量和actual_number以便重置按钮之间的计数器。

      var counter = 0; var num = ""; $(".button").on("click", function (e) { var $this = $(this); actual_number = $(this).children(".num").text(); if(counter < 3 && num == actual_number ){ $(".letter").removeClass('highlighted'); $(this).children(".letter").eq(counter).addClass('highlighted'); var result = $(this).children(".letter").eq(counter).text(); $(".result").text(result); counter++; }else{ counter=0; num = $(this).children(".num").text(); $(".letter").removeClass('highlighted'); $(this).children(".letter").eq(counter).addClass('highlighted'); var result = $(this).children(".letter").eq(counter).text(); $(".result").text(result); counter++; } }) 
      .button { float: left; width: 100px; height: 100px; margin: 20px 20px; } .num { font-size: 50px; } .result { font-size: 100px; color: blue; padding: 50px; } .highlighted { border: 1px solid red; } 
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="result">A</div> <div class="keypad"> <div class="button"> <div class="num">1</div> <span class="letter">A</span> <span class="letter">B</span> <span class="letter">C</span> </div> <div class="button"> <div class="num">2</div> <span class="letter">D</span> <span class="letter">E</span> <span class="letter">F</span> </div> <div class="button"> <div class="num">3</div> <span class="letter">G</span> <span class="letter">H</span> <span class="letter">I</span> </div> </div> 

Hope it helps 希望能帮助到你

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

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