2016-04-14 3 views
2

このコードの何が問題なのですか?私は二重の茶色のボーダーまたは固体青い枠を設定する条件に依存し、私が使用しているものの状態は関係ありません... if($(this).has("#tt"))またはif(($(this).has("ssssssss")))または何が、それは常に二重の茶色の境界線を入れ続け関数をパラメータとして使用するラップメソッド

のjQuery :

$(document).ready(function() { 
    $(".divvv").wrap(function() { 
     if ($(this).has("#tt")) { 
      return $("<div/>").css("border","double thick brown") 
     } 
     else { 
      return $("<div/>").css("border","solid blue") 
     } 
    }) 
}) 

HTML:

<div id="firstdiv" class="divvv"> 
<label class ="lab" id="tt" for="two">2</label> 
    <input type="checkbox" value="2" id="two" /> 
    <label class ="lab" for="four">4</label> 
    <input type="checkbox" value="4" id="four" /> 
</div> 

<div class="divvv" id="secdiv"> 
<button id="bu">Here</button><button id="bubu"> Button</button> 
    <label id="second">This is the <span style="color:aqua">second</span> label</label> 
    <label>This is the third label</label> 
     </div> 

答えて

3

はこれを試してみてください:.has()が真か偽オブジェクトを返し、ありません。 .has().lengthは1を返します。つまり、一致したオブジェクトがある場合はtrue、そうでない場合は0です。 、一致がないの存在するかどうかを毎回オブジェクトを返す

$(document).ready(function() { 
    $(".divvv").wrap(function() { 
     if ($(this).has("#tt").length) { 
      return $("<div/>").css("border","double thick brown") 
     } 
     else { 
      return $("<div/>").css("border","solid blue") 
     } 
    }) 
}) 

HTML

<div id="firstdiv" class="divvv"> 
<label class ="lab" id="tt" for="two">2</label> 
    <input type="checkbox" value="2" id="two" /> 
    <label class ="lab" for="four">4</label> 
    <input type="checkbox" value="4" id="four" /> 
</div> 

<div class="divvv" id="secdiv"> 
<button id="bu">Here</button><button id="bubu"> Button</button> 
    <label id="second">This is the <span style="color:aqua">second</span> label</label> 
    <label>This is the third label</label> 
     </div> 

.has():コード

$(document).ready(function() { 
 
    $(".divvv").wrap(function() { 
 
     if ($(this).has("#tt").length) { 
 
      return $("<div/>").css("border","double thick brown") 
 
     } 
 
     else { 
 
      return $("<div/>").css("border","solid blue") 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="firstdiv" class="divvv"> 
 
<label class ="lab" id="tt" for="two">2</label> 
 
    <input type="checkbox" value="2" id="two" /> 
 
    <label class ="lab" for="four">4</label> 
 
    <input type="checkbox" value="4" id="four" /> 
 
</div> 
 

 
<div class="divvv" id="secdiv"> 
 
    <button id="bu">Here</button><button id="bubu"> Button</button> 
 
    <label id="second">This is the <span style="color:aqua">second</span> label</label> 
 
    <label>This is the third label</label> 
 
</div>

2

使用下記参照しかし.has()。length一致しない場合は0を返し、一致する場合は1を返します。

DEMO

関連する問題