如何检测div没有使用jQuery的子类

时间:2016-12-10 17:02:26

标签: javascript jquery html css

我有一些代码如下: -



Sub Splitter()

Dim i As Integer
Dim Text As String
Dim Item As Variant

Text = ActiveCell.Select
Item = Split(ActiveCell, "-")

For i = 0 To UBound(Item)
    Cells(3, i + 2).Value = Item(i)
Next i

If Range("B3").Value = "CHOP" Then
    Range("D3").Value = "chopsticks"
ElseIf Range("B3").Value = "NAPK" Then
    Range("D3").Value = "napkins"
ElseIf Range("B3").Value = "RICE" Then
    Range("D3").Value = "white rice"
ElseIf Range("B3").Value = "SOYS" Then
    Range("D3").Value = "soy sauce"
ElseIf IsEmpty(Range("D3").Value) = True Then
    Range("D3").Value = "other"
End If

End Sub

if($('.container').has(':not(.classB)')){
  //$(this).addClass('added'); - this code didnt work,i dont know why @@
  $('.container').addClass('added');
}
//if($('.container').find('.classB').length != 0)

.added .classA{background: red;}




我已经尝试了一些方法来检测没有classB但是它没有用。这里有两个div受影响(添加新类)。这应该只适用于div class' containter'没有classB(第二个div)。

先谢谢!

3 个答案:

答案 0 :(得分:1)

以下可能有效:

$('.container').each(function(index) {
    // if containing at least one descendant with classB
    if( $(this).find('.classB').length > 0 ) {
        console.log('class B found in container n°'+ index);
        // do your stuff..
    }
    // if not containing any descendants with classB
    if( $(this).find('.classB').length == 0 ) {
        console.log('class B NOT found in container n°'+ index);
        // do your stuff
    }
});

基本上,它将遍历所有容器,在每个容器中选择所有带有classB的后代,并使用length来检查它是否设法找到了一些。

请注意.find()将在每个后代(深度超过1级)中查找classB,如果要停止查看级别1,请改用.children()。 < / p>

希望我能提供帮助:)

答案 1 :(得分:1)

为什么不测试react-native run-android C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:6: error: cannot find s mbol import com.facebook.react.ReactApplication; ^ symbol: class ReactApplication location: package com.facebook.react C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:9: error: cannot find s mbol import com.facebook.react.ReactNativeHost; ^ symbol: class ReactNativeHost location: package com.facebook.react C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:17: error: cannot find ymbol public class MainApplication extends Application implements ReactApplication { ^ symbol: class ReactApplication C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:19: error: cannot find ymbol private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { ^ symbol: class ReactNativeHost location: class MainApplication C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:35: error: cannot find ymbol public ReactNativeHost getReactNativeHost() { ^ symbol: class ReactNativeHost location: class MainApplication C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainActivity.java:5: error: MainActivity is ot abstract and does not override abstract method getPackages() in ReactActivity public class MainActivity extends ReactActivity { ^ C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:19: error: cannot find ymbol private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { ^ symbol: class ReactNativeHost location: class MainApplication C:\Users\y\RNapp\android\app\src\main\java\com\rnapp\MainApplication.java:34: error: method does ot override or implement a method from a supertype @Override ^ 8 errors :app:compileDebugJavaWithJavac FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 6.098 secs 的反面?

此外,您当前的CSS选择器:.hasClass只会影响具有children()类的元素,这些元素是具有类.added .classA的元素的后代,而这些元素都不是您的元素。

&#13;
&#13;
classA
&#13;
added
&#13;
// Loop through the containers
$('.container').each(function(){
 
  // If the current container doesn't have any children that use classB...
  if(!$(this).children().hasClass('classB')){
    // Add the added calss to the child in question
    $(this).children().addClass('added');
  }

});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你可以走另一条路,从任何匹配的孩子开始

&#13;
&#13;
$('.classB').parent('.container').addClass('added');
&#13;
.added { background: red; }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <div class="classA">AA</div>
    <div class="classB">BB</div>
</div>
<div class="container">
    <div class="classA">AA</div>
</div>
&#13;
&#13;
&#13;