【问题标题】:jQuery has() not working as intendedjQuery has() 没有按预期工作
【发布时间】:2018-03-17 09:42:12
【问题描述】:

也许我理解错了,但这 has() 在这种情况下不应该工作吗?我做错了什么?

  $("#count").on("click", function(){
    $(".item").each(function( index, element ){
      $(element).text( index + 1 );

      /*** this line does nothing ****/
      $(element).has(".special").css("background", "black");

      if( index + 1  == 4  || index + 1  == 8 ){
         $(this).css("background", "#A5C663");
      }
    })
  })

HTML:

<div id="grid">
    <div class="item"></div>
    <div class="item special"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item special"></div>
    <div class="item"></div>
  </div>

这也不起作用:

$(this).has(".special").css("background", "black"); 

或者:

$(".item).has(".special").css("background", "black"); 

【问题讨论】:

    标签: jquery iteration


    【解决方案1】:

    has 替换为is

    • is是关于这组元素的
    • has是关于后代的

    【讨论】:

    • 我明白了。例如,我在“.item”中添加了一个带有“特殊”类的段落。谢谢你。在允许的情况下接受。
    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多