2014-01-16 20 views
0
<div id='QuestionText'> 
    <p>This is a simple example </p> 
    <span> I am asking the question </span> 
</div> 

我得到div的HTML内容在下面的行如何检查是否选择存在于跨度或不jQuery的

VAR existing_lesson = $( '#QuestionText')。HTML ();

我得到了 '问题' 作为我在下面一行

VAR getselectedarea = $ .selection()选择;

现在我想要找的选择是否是存在于span元素或者不意味着如果我选择simple example为我的选择,我应该得到,这是存在于HTML标签,如果我选择the question我的选择我应该知道它存在于哪个html标签中。

回答

1
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; 
    }); 
+1

为什么在这种情况下不使用“:contains”语句?性能问题? – cubitouch

0

另一种方式来做到这一点(以替代猎人的indexOf做法)是一个正则表达式.test()

var foundInSpan = new RegExp(text).test($("#QuestionText > span").text()); 

jsPerf显示的性能是更多或少洗:http://jsperf.com/find-text-test

相关问题