如何使用jquery在<p>标签内找到脚本标签?

时间:2015-05-21 06:07:20

标签: jquery html

我在html <p>标签中获取了脚本标签。

示例:

<p>
   <script>
     function essb_window1149322389(oUrl, oService) {
       essb_window_stat(oUrl, oService, 55311);  
     };
   </script>
</p>

使用jquery在<script>标记内是否存在<p>标记是他们的任何查找方式。

3 个答案:

答案 0 :(得分:4)

尝试使用jQuery find()

$('p').find('script').length

注意,这将搜索直接和嵌套的子项。如果您只想要直接脚本子项,请使用children(),因为性能会有很小的提升。

$('p').children('script').length
//equivalent to $('p script').length

DEMO

答案 1 :(得分:2)

您可以使用Descendant Selector (“ancestor descendant”)Child Selector (“parent > child”),使用带有选择器的长度来判断元素是否存在。如果length大于0,则存在脚本标记。

if($('p script').length)
{
     //exists
}

答案 2 :(得分:2)

常规选择器(按节点类型的父/子)应该可以工作:

if ($("p script").length) ...
相关问题