很震惊这个问题没有出现在相关问题列表中。有一些类似的东西,但我想知道:
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/