用jQuery解析HTML字符串 [英] Parsing HTML String with jQuery

查看:383
本文介绍了用jQuery解析HTML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在HTML字符串上使用jQuery选择器?我正在尝试获取div foo的html内容.

Is it possible to use a jQuery selector on an HTML string? I'm trying to get the html contents of the div, foo.

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

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

How can I get the html contents of foo and of bar in two different statements?

推荐答案

var code = $("<div id='foo'>1</div><div id='bar'>2</div>");

code.each(function() {
    alert( $(this).html() );
})

这篇关于用jQuery解析HTML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆