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

javascript - How can I apply a jQuery function to all elements with the same class.? - Stack Overflow

programmeradmin0浏览0评论

JQuery return value for all same name class = 2.53 (first element value applied for all Span)

How do I get different values?

(Edit:) HTML code:

<div class='ratingInfo'>
    <table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
      <tbody>
        <tr>
          <td>
             <div class='review-rating'>10</div>
          </td>
          <td>
             <div class='stars1'></div>
          </td>
        </tr>
      </tbody>
    </table>
</div>


<div class='ratingInfo'>
    <table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
       <tbody>
          <tr>
            <td>
                <div class='review-rating'>1</div>
            </td>
            <td>
              <div class='stars1'></div>
            </td>
          </tr>
        </tbody>
     </table>
 </div>

JavaScript

 $( document ).ready(function() {
    $( ".stars1" ).html("<span class='stars'>"+$('.review-rating').text()+"</span>");
    $('span.stars').stars();
 });

 $.fn.stars = function() {
    return $(this).each(function() {
        $(this).html($('<span />').width(Math.max(0, (Math.min(5,   parseFloat($(this).html())))) * 16));
    });
 }

JQuery return value for all same name class = 2.53 (first element value applied for all Span)

How do I get different values?

(Edit:) HTML code:

<div class='ratingInfo'>
    <table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
      <tbody>
        <tr>
          <td>
             <div class='review-rating'>10</div>
          </td>
          <td>
             <div class='stars1'></div>
          </td>
        </tr>
      </tbody>
    </table>
</div>


<div class='ratingInfo'>
    <table class='rating_table' border='0' cellpadding='0' cellspacing='0'>
       <tbody>
          <tr>
            <td>
                <div class='review-rating'>1</div>
            </td>
            <td>
              <div class='stars1'></div>
            </td>
          </tr>
        </tbody>
     </table>
 </div>

JavaScript

 $( document ).ready(function() {
    $( ".stars1" ).html("<span class='stars'>"+$('.review-rating').text()+"</span>");
    $('span.stars').stars();
 });

 $.fn.stars = function() {
    return $(this).each(function() {
        $(this).html($('<span />').width(Math.max(0, (Math.min(5,   parseFloat($(this).html())))) * 16));
    });
 }
Share Improve this question edited Nov 19, 2014 at 21:06 Roko C. Buljan 207k41 gold badges328 silver badges340 bronze badges asked Nov 19, 2014 at 20:07 rf jmrf jm 331 silver badge5 bronze badges 3
  • P.S. In your $.fn.stars function this is already a jQuery object. You can just do return this.each(function(){ //... });. – gen_Eric Commented Nov 19, 2014 at 21:07
  • That wasn't a solution to the problem, I was just pointing it out. – gen_Eric Commented Nov 19, 2014 at 21:15
  • Working fine with out table why is that.? – rf jm Commented Nov 19, 2014 at 21:16
Add a ment  | 

1 Answer 1

Reset to default 10

Because $('.review-rating').text() is grabbing the first element every time, it does not know you want the one that is beside the element. You need to code it to look there.

$( ".stars1" ).each( function () {
    var star = $(this);
    star.html("<span class='stars'>" + star.prev('.review-rating').text() + "</span>");
});

with the new HTML code, the above will not work since the elements are NOT siblings. That is why it is important to have the actual code! You need to look for a mon parent and find the element within it, in this case you have the TR that contains both. So to get the parent, use closest()

$( ".stars1" ).each( function () {
    var star = $(this);
    var rating = star.closest("tr").find(".review-rating").text();
    star.html("<span class='stars'>" + rating + "</span>");
});
发布评论

评论列表(0)

  1. 暂无评论