37-解决方案
3.9.2 解决方案
可以结合使用核心属性 .selector 和 m.context ,重新创建原始查询。必须将两个核心结合使用,因为函数或者插件中的所有查询并不都在默认文档上下文中:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Chapter 3 - Recipe 9 - Determining the exact query that was used</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
(function($){
$.fn.ShowQuery = function(i) {
alert("$(\""+ $(this).selector + "\", " + $(this).context +")");
if (i < 3)
{
$($(this).selector, $(this).context).ShowQuery(i+1);
}
};
$("div").ShowQuery(1);
})(jQuery);
//-->
</script>
</head>
<body>
<div>
This is a div.
</div>
</body>
</html>
图3-9展示了输出效果。
