我正在尝试为jquery编写一些插件,因此作为起点,我开始介绍文档中显示的基本示例。(
here
)这是我在名为jquery.test.js的JS文件中拥有的代码:
(function($) {
$.fn.myPlugin = function(settings) {
var config = {'foo': 'bar'};
if (settings) $.extend(config, settings);
this.each(function() {
// element-specific code here
alert('found P tag');
});
return this;
});
})(jQuery);
这里是HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<title>Plugin Test</title>
<script type="text/javascript" src="/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/jquery.test.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('p').myPlugin();
});
</script>
</head>
<body>
<p>asdgasdgasg</p>
</body>
</html>
我希望这会对页面中的每个段落标签都弹出一个警报。但是,当我加载页面时,什么也不会发生。Firebug给了我两个错误:
missing ; before statement
http://playground.darthvader.com/jquery.test.js
Line 16
$("p").myPlugin is not a function
http://playground.darthvader.com/ol.html
Line 11
我很困惑。有人能看到我在这里做错了什么吗?
编辑:结果发现jquery文档中有一个拼写错误(很好地捕捉到了ghommey),我正从中复制和粘贴代码。我联系了Docs网站的管理员,现在已经修复了。