【问题标题】:Find closest element from an Each function in jQuery从 jQuery 中的 Each 函数中查找最近的元素
【发布时间】:2014-07-30 17:01:22
【问题描述】:

我有一个呈现测验分数的页面。我想轻松地在分数旁边显示响应文本。当只显示一个分数时,我设法轻松地做到了这一点,但是我的用户有一个包含多个分数的仪表板,我想为每个分数显示这个响应文本,但我无法让它工作。我试过 (find)、(closest)、(prev) 等。这是我得到的:

HTML

<div class="content">
    <div class="quiz_score_response">response</div>
    <div class="quiz_score_possible">You got <em>1</em> points.</div>
</div>

<div class="content">
    <div class="quiz_score_response">response</div>
    <div class="quiz_score_possible">You got <em>2</em> points.</div>
</div>

<div class="content">
    <div class="quiz_score_response">response</div>
    <div class="quiz_score_possible">You got <em>3</em> points.</div>
</div>

<div class="content">
    <div class="quiz_score_response">response</div>
    <div class="quiz_score_possible">You got <em>4</em> points.</div>
</div>

和 JS

$(".quiz_score_possible em").each(function(){

    if ($(this).is(":contains('1')")) {$(this).closest(".quiz_score_response").text("You Suck");}
    if ($(this).is(":contains('2')")) {$(this).closest(".quiz_score_response").text("You're OK");}
    if ($(this).is(":contains('3')")) {$(this).closest(".quiz_score_response").text("You're Good");}
    if ($(this).is(":contains('4')")) {$(this).closest(".quiz_score_response").text("You're Awesome");}

});

还有我的 jsfiddle http://jsfiddle.net/9FvM8/3/

【问题讨论】:

  • .closest() 搜索所选元素的祖先,而不是兄弟。

标签: jquery find each closest


【解决方案1】:

由于您获得的是数字,您可以将响应保存在一个数组中,然后以相反的方式进行,从要更改的文本元素开始,然后在 text 的回调中获取数字和匹配的响应

var arr = ["", "You Suck", "You're OK", "You're Good", "You're Awesome"];

$(".quiz_score_response").text(function() {
    return arr[+$(this).next('div').find('em').text()];
});

FIDDLE

【讨论】:

    【解决方案2】:
    $(".quiz_score_possible > em").each(function(){        
        if ($(this).is(":contains('1')")) {$(this).parent().parent().find(".quiz_score_response").text("You Suck");}
        if ($(this).is(":contains('2')")) {$(this).parent().parent().find(".quiz_score_response").text("You're OK");}
        if ($(this).is(":contains('3')")) {$(this).parent().parent().find(".quiz_score_response").text("You're Good");}
        if ($(this).is(":contains('4')")) {$(this).parent().parent().find(".quiz_score_response").text("You're Awesome");}  
    });
    

    演示:

    http://jsfiddle.net/z2vDm/

    【讨论】:

      【解决方案3】:

      当然你不能这样做,你应该使用

      $(this).parent().siblings(".quiz_score_response");
      

      所以代码会喜欢

      $(".quiz_score_possible em").each(function(){  
          if ($(this).is(":contains('1')")) {$(this).parent().siblings(".quiz_score_response").text("You Suck");}
          if ($(this).is(":contains('2')")) {$(this).parent().siblings(".quiz_score_response").text("You're OK");}
          if ($(this).is(":contains('3')")) {$(this).parent().siblings(".quiz_score_response").text("You're Good");}
          if ($(this).is(":contains('4')")) {$(this).parent().siblings(".quiz_score_response").text("You're Awesome");}
      
      });
      

      【讨论】:

      • 他们不是兄弟姐妹。居然是大叔。
      【解决方案4】:
      $(".quiz_score_possible em").each(function(){
          if ($(this).is(":contains('1')")) {$(this).parent().parent().find(".quiz_score_response").text("You Suck");}
          if ($(this).is(":contains('2')")) {$(this).parent().parent().find(".quiz_score_response").text("You're OK");}
          if ($(this).is(":contains('3')")) {$(this).parent().parent().find(".quiz_score_response").text("You're Good");}
          if ($(this).is(":contains('4')")) {$(this).parent().parent().find(".quiz_score_response").text("You're Awesome");}
      });
      

      【讨论】:

        【解决方案5】:

        替换:

        if ($(this).is(":contains('1')")) {$(this).closest(".quiz_score_response").text("You're OK");}
        

        与:

        if ($(this).is(":contains('1')")){$(this).parent().parent().find(".quiz_score_response").text("You Suck");}
        

        或更好:

        if ($(this).text() == '1') {$(this).parent().parent().find(".quiz_score_response").text("You Suck");}
        

        使用 text() 的精确匹配将使代码更高效,因为将来用户点将是多个数字的组合,而不是一个。

        Update JS Fiddle

        【讨论】:

          猜你喜欢
          • 2011-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多