<div id='QuestionText'>
<p>This is a simple example </p>
<span> I am asking the question </span>
</div>
我在以下行中获取了div 的 html 内容
var existing_lesson = $('#QuestionText').html();
我在下一行中得到"问题"作为我的选择
现在我想找出选择是否存在于 span 元素中意味着如果我选择var getselectedarea = $.selection();
simple example
作为我的选择,我应该得到它存在于哪个 html 标签中,如果我选择the question
作为我的选择,我应该得到它存在于哪个 html 标签中。
var foundInSpan = $("#QuestionText > span").text().indexOf(text) > -1;
var foundInP = $("#QuestionText > p").text().indexOf(text) > -1;
或者,您可以使用grep
查找包含该文本的所有标签:
var tags = $("#QuestionText").children().grep(
function(){
return $element.text().indexOf(text) > -1;
});
另一种方法(作为猎人indexOf
方法的替代方案)是使用正则表达式.test()
:
var foundInSpan = new RegExp(text).test($("#QuestionText > span").text());
jsPerf显示性能或多或少是一种洗涤:http://jsperf.com/find-text-test