简体   繁体   English

在jQuery addClass()中使用变量

[英]Using a variable with jQuery addClass()

I'm new to JS and jQuery but I've been trying to use the addClass() and removeClass with some Javascript variables and don't know what I'm doing wrong. 我是JS和jQuery的新手,但是我一直在尝试将addClass()removeClass与一些Javascript变量一起使用,并且不知道我在做什么错。

I have this 1–5 star rating system. 我有这个1-5星评分系统。

I want it to change classes when hovering and save rating when I click. 我希望它在悬停时更改类并在单击时保存评分。

The HTML looks like this: HTML看起来像这样:

 <span class="icon-rating-empty" id="blob1"></span> 

and so on for blobs 2–5. 等等,对于斑点2-5。

The JS I currently have is 我目前拥有的JS是

 var blob1 = document.getElementById('blob1'); var blob2 = document.getElementById('blob2'); var blob3 = document.getElementById('blob3'); var blob4 = document.getElementById('blob4'); var blob5 = document.getElementById('blob5'); var rating = 0; blob1.addEventListener('hover', function() { $(blob1).addClass("icon-rating-full").addClass("green-blob").removeClass("icon-rating-empty") }, function() { $(blob1).addClass("icon-rating-empty").removeClass("green-blob").removeClass("icon-rating-full") }); blob1.addEventListener('click', function() { rating = 1; }); 

This doesn't work—I'm sure I don't know how to handle variables with the jQuery, but can't find any article on what I'm supposed to do. 这是行不通的-我确定我不知道如何使用jQuery处理变量,但是找不到关于我应该做什么的文章。

I've tested it and if I use $("#blob1") instead of $(blob1) it works, but I'd like to use the variables because I have more in plan. 我已经测试过了,如果我用$("#blob1")代替$(blob1)可以工作,但是我想使用变量,因为我有更多的计划。

You're not far off. 你不远。 You will need to set up two sets of event listeners: one for when the user clicks on a star, and one for when the user hovers over a star. 您将需要设置两组事件侦听器:一组用于用户单击星星时,另一组用于用户将鼠标悬停在星形上。

I suggest saving the "rating" as a variable. 我建议将“评级”另存为变量。 When the user clicks a star, you adjust the rating. 当用户单击星号时,您可以调整评分。 I used a data attribute to keep track of the value of each star. 我使用了data属性来跟踪每颗星星的值。 After adjusting the rating, you loop through the elements and add or remove classes accordingly. 调整等级后,您可以遍历元素并相应地添加或删除类。

Hovering adds a layer of complexity. 悬停增加了一层复杂性。 Every time there is mouseover, you will need to loop over the star elements and update the classes as though that was the new rating. 每次将鼠标悬停在上面时,您都需要遍历star元素并更新类,就像那是新的评分一样。 When you mouseout, you will update the classes again according to the rating that you saved. 当您将鼠标移出时,将根据保存的等级再次更新类。

var rating = 0;
var stars = $('li');

// Set up listeners
$('li').on('click', function(){
    rating = parseInt($(this).data('val'));
  drawStars(rating);
});

$('li').hover(
    function(){
    tempRating = parseInt($(this).data('val'));
    drawStars(tempRating);
  },
  function(){
    drawStars(rating);
  }
);

// This function loops through the "star" elements and adds and removes a 'selected' class
function drawStars(numStars){
    // Reset the colour by removing the selected class from all elements
  for(var i = 0; i < stars.length; i++){
    $(stars[i]).removeClass('selected');
  }
  // Add a selected class to some of the elements
  for(var i = 0; i < numStars; i++){
    $(stars[i]).addClass('selected');
  }
}

See this jsfiddle: https://jsfiddle.net/ffz5y9fm/5/ 看到这个jsfiddle: https ://jsfiddle.net/ffz5y9fm/5/

Untested: 未经测试:

<span class="rating">
<span data-star="0">STAR</span>
<span data-star="1">STAR</span>
<span data-star="2">STAR</span>
<span data-star="3">STAR</span>
<span data-star="4">STAR</span>
<span data-star="5">STAR</span>
</span>

<script>
try{
var elements = document.querySelectorAll('.rating'), 
    listener = function(e){
       var a = this, parent = a.parentNode, value = parseInt( a.getAttribute('data-star') );
       // reset
       parent.classList = 'rating';
       // add class
       parent.classList.add( 'star-'+value );
    };


// get all elements and attach listener
for( var i = 0; i < elements.length; ++i ){
  var current = elements[i], childs = current.children;
    for( var ii = 0; ii < childs.length; ++ii ){
      childs[ii].addEventlistener('mouseenter',listener,false);
    }
}

} catch(e){ console.log('error',e); }
</script>

If something doesn't work just report here. 如果某些方法不起作用,请在此处报告。

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

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