jquery - 使用 JQuery 解析包含 html 的字符串

标签 jquery html

很震惊这个问题没有出现在相关问题列表中。有一些类似的东西,但我想知道:

string s = "<p>Cats</p><div id='fishbiscuits'>myValue</div>";

我想使用 JQuery 获取 myValue。

我该怎么做?

最佳答案

使用 jQuery 函数创建对象,然后您可以像使用任何其他元素集合一样使用它。这些中的任何一个都应该可以正常工作:

// Using filter()
$(s).filter("#fishbiscuits").text();

// Wrapping with a div and using find()
$('<div>' + s + '</div>').find("#fishbiscuits").text();

// Wrapping with a div and using selector context
$('#fishbiscuits', '<div>' + s + '</div>').text();

// Creating a div, setting the html and then using find
$('<div>').html(s).find("#fishbiscuits").text();

关于jquery - 使用 JQuery 解析包含 html 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3307402/

相关文章:

javascript - 在文档中添加新元素后,jQuery 显示不起作用

javascript - 使用 ng-options 在选择下拉列表中显示对象数据

java - 使用 java 的 Http 请求的 400 错误请求

html - 一个 div 中的两个 div,每两个中有 img,如何自动调整大小但保持行形成?

javascript - 一种输入 - 两种风格?左侧光标 :text, 右侧光标 :pointer?

javascript - 在嵌套循环中从 $.get() 返回数组

ajax - jquery mobile 取消 pagebeforeshow 事件

javascript - Jquery:表内行的内联编辑

html - 样式跨越多行

javascript - 无法使用javascript更改标题字体大小