Javascript 检查html元素的数量,然后运行函数

Javascript 检查html元素的数量,然后运行函数,javascript,jquery,Javascript,Jquery,我有三个不同的场景,其中span元素出现 无子跨度元素: <div class="text" contenteditable="true" id="example"> <div class="outside">Type here</div> </div> <div class="text" contenteditable="true" id="example"> <div class="outside">Type he

我有三个不同的场景,其中span元素出现

  • 无子跨度元素:

    <div class="text" contenteditable="true" id="example">
     <div class="outside">Type here</div>
    </div>
    
    <div class="text" contenteditable="true" id="example">
     <div class="outside">Type here <span class="inside"> please.</span></div>
    </div>
    
    因为span类可以出现一次或多次,或者根本不存在,所以我想检查span是否存在。然后根据span元素出现的次数,我需要为所有span元素运行函数

    我将如何实现这一点

    我想检查是否存在span

    可以通过检查选择器的length属性来实现这一点:

     var $spans = $('span.inside');
     var numberOfSpans = $spans.length;
    
    if (numberOfSpans > 5) { // 5 just an example
        $spans.each(function() {
            // do something with the span here...
        });
    }
    
    根据span元素出现的次数,我需要为所有span元素运行函数

    这是一个简单的
    if
    语句,您可以使用
    each()
    循环选择器中的元素:

     var $spans = $('span.inside');
     var numberOfSpans = $spans.length;
    
    if (numberOfSpans > 5) { // 5 just an example
        $spans.each(function() {
            // do something with the span here...
        });
    }
    
    我想检查是否存在span

    可以通过检查选择器的length属性来实现这一点:

     var $spans = $('span.inside');
     var numberOfSpans = $spans.length;
    
    if (numberOfSpans > 5) { // 5 just an example
        $spans.each(function() {
            // do something with the span here...
        });
    }
    
    根据span元素出现的次数,我需要为所有span元素运行函数

    这是一个简单的
    if
    语句,您可以使用
    each()
    循环选择器中的元素:

     var $spans = $('span.inside');
     var numberOfSpans = $spans.length;
    
    if (numberOfSpans > 5) { // 5 just an example
        $spans.each(function() {
            // do something with the span here...
        });
    }
    
    var length=$('span.inside').length;
    如果(长度>0){
    $('span.inside')。每个(函数(){
    var len=$(this.get(0).nextSibling.length;
    控制台日志(len);
    })
    }
    
    请在这里打字,谢谢
    请说出你的名字。谢谢
    
    var length=$('span.inside').length;
    如果(长度>0){
    $('span.inside')。每个(函数(){
    var len=$(this.get(0).nextSibling.length;
    控制台日志(len);
    })
    }
    
    请在这里打字,谢谢
    请说出你的名字。谢谢
    
    关于
    $(“span.inside”).length(长度)
    ?使用带长度的类检查span(长度)
    $(“span.inside”).length(长度)
    ?使用带长度的类检查span