当前位置:嗨网首页>书籍在线阅读

37-解决方案

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

3.9.2 解决方案

可以结合使用核心属性 .selectorm.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展示了输出效果。

9.png

图3-9 代码输出(警告框)