代码之家  ›  专栏  ›  技术社区  ›  Scott

用jQuery解析HTML字符串

  •  3
  • Scott  · 技术社区  · 15 年前

    var code = "<div id='foo'>1</div><div id='bar'>2</div>";
    alert( $(code).html( ) ); // returns 1 (not sure how...)
    

    如何在两个不同的语句中获取foo和bar的html内容?

    2 回复  |  直到 15 年前
        1
  •  6
  •   meder omuraliev    15 年前
    var code = $("<div id='foo'>1</div><div id='bar'>2</div>");
    
    code.each(function() {
        alert( $(this).html() );
    })
    
        2
  •  2
  •   Roatin Marth    15 年前

    map

    var code = "<div id='foo'>1</div><div id='bar'>2</div>";
    var html = $(code).map(function() { return $(this).html() });
    html[0]; // "1"
    html[1]; // "2"
    

    这就是你的意思吗?