最简单的形式(如果我正确理解):
var s = "
Hello World!
By Mars
";
var o = $(s);
var text = o.text();
或者您可以使用具有搜索上下文的条件选择器:
// load string as object,wrapped in an outer container to use for search context
var o = $("
Hello World!
By Mars
");
// sets the context to only look within o; otherwise,this will return all P tags
var tags = $("P",o);
tags.each(function(){
var tag = $(this); // get a jQuery object for the tag
// do something with the contents of the tag
});
如果您正在解析大量的HTML(例如,解释屏幕截图的结果),请使用服务器端HTML解析库,而不是jQuery(这里有关于HTML解析的帖子).
@[email protected]
@[email protected]
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。