Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery.each对匹配对象的子集进行不同的操作_Jquery_Each - Fatal编程技术网

如何使用jQuery.each对匹配对象的子集进行不同的操作

如何使用jQuery.each对匹配对象的子集进行不同的操作,jquery,each,Jquery,Each,我正在使用最新的jQuery(通过Google CDN)并使用以下代码构建TOC: $("#infoArticles h2, #infoArticles h3").each(function(i) { var current = $(this); current.attr("id", "title" + i); $("#toc").append("<li><a id='link" + i + "' href='#title" + i + "' title

我正在使用最新的jQuery(通过Google CDN)并使用以下代码构建TOC:

$("#infoArticles h2, #infoArticles h3").each(function(i) {
    var current = $(this);
    current.attr("id", "title" + i);
    $("#toc").append("<li><a id='link" + i + "' href='#title" + i + "' title='" + current.attr("tagName") + "'>" + current.html() + "</a></li>");
});
$(“#infoArticles h2,#infoArticles h3”)。每个(功能(i){
var current=$(此);
当前属性(“id”、“标题”+i);
$(“#toc”)。追加(“
  • ”); });
  • 工作是一种享受

    我希望每个循环对匹配的h2和匹配的h3进行不同的处理,以便我可以向由h2产生的li添加一个类


    任何有帮助的评论,非常感谢

    检查
    节点名
    标记名

    $("h2").each(function(i){
      alert(this.nodeName + " " + i); // Alerts 'H2 0,' 'H2 1,' etc.
    });
    
    $(“#infoArticles h2,#infoArticles h3”)。每个(功能(i){
    var current=$(此);
    当前属性(“id”、“标题”+i);
    如果(this.tagName==“H2”){
    //氢
    }否则{
    //h3
    }
    $(“#toc”)。追加(“
  • ”); });
  • 您可以用代码创建一个函数,并使用不同的参数调用它:

    $("#infoArticles h2").each(function(i) {
        myFunction("ma-classe");
    });
    $("#infoArticles h3").each(function(i) {
        myFunction();
    });
    
    function myFunction(class=""){
       var current = $(this);
        current.attr("id", "title" + i);
        $("#toc").append("<li class='"+class+"'><a id='link" + i + "' href='#title" + i + "' title='" + current.attr("tagName") + "'>" + current.html() + "</a></li>");
    }
    
    $(“#infoArticles h2”)。每个(函数(i){
    myFunction(“ma类”);
    });
    $(#infoArticles h3”)。每个(功能(i){
    myFunction();
    });
    函数myFunction(class=”“){
    var current=$(此);
    当前属性(“id”、“标题”+i);
    $(“#toc”).append(“
  • ”); }
    请注意,Append非常昂贵,因此,为了优化,您应该将Append()函数移出循环,只执行一次,而不是针对每个选定的元素

    var htmlToInsert = null;
    $("#infoArticles h2, #infoArticles h3").each(function(i) {
        var current = $(this);
        current.attr("id", "title" + i);
        if(this.tagName == "H2"){
          //h2
        }else{
          //h3
        }
        htmlToInsert += "<li><a id='link" + i + "' href='#title" + i + "' title='" + current.attr("tagName") + "'>" + current.html() + "</a></li>";
    
    });
    $("#toc").append(htmlToInsert);
    
    var htmlToInsert=null;
    $(“#信息文章h2,#信息文章h3”)。每个(功能(i){
    var current=$(此);
    当前属性(“id”、“标题”+i);
    如果(this.tagName==“H2”){
    //氢
    }否则{
    //h3
    }
    htmlToInsert+=“
  • ”; }); $(“#toc”)。追加(插入);
    欢迎来到StackOverflow。第一个问题很好,哇。完全被响应的质量和速度冲昏头脑。“谢谢”似乎不够,但谢谢大家的回答。我很高兴我们给大家留下了良好的第一印象。下一步,选择并回答,然后继续工作;)
    var htmlToInsert = null;
    $("#infoArticles h2, #infoArticles h3").each(function(i) {
        var current = $(this);
        current.attr("id", "title" + i);
        if(this.tagName == "H2"){
          //h2
        }else{
          //h3
        }
        htmlToInsert += "<li><a id='link" + i + "' href='#title" + i + "' title='" + current.attr("tagName") + "'>" + current.html() + "</a></li>";
    
    });
    $("#toc").append(htmlToInsert);