WordPress 中的 Jquery 网格覆盖
我正在添加这个在静态 html 站点中工作的简单插件,并尝试将其添加到基于 960 gs 的 WordPress 开发站点中。 jquery 代码链接是正确的,但控制台给了我这个错误“Uncaught TypeError: Cannot call method 'addGrid' of null” 我从这个教程中得到了代码http://www.badlydrawntoy.com/2009/04/21/960gs-grid- overlay-a-jquery-plugin/
这是我正在使用的代码
/*<![CDATA[*/
// onload
$(function() {
$("body").addGrid(12, {img_path: 'img/'});
});
/*]]>*/
这是插件的代码
/*
* @ description: Plugin to display 960.gs gridlines See http://960.gs/
* @author: badlyDrawnToy sharp / http://www.badlydrawntoy.com
* @license: Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/
* @version: 1.0 20th April 2009
*/
(function($){$.fn.addGrid=function(cols,options){var defaults={default_cols:12,z_index:999,img_path:'/images/',opacity:.6};var opts=$.extend(defaults,options);var cols=cols!=null&&(cols===12||cols===16)?cols:12;var cols=cols===opts.default_cols?'12_col':'16_col';return this.each(function(){var $el=$(this);var height=$el.height();var wrapper=$('<div id="'+opts.grid_id+'"/>').appendTo($el).css({'display':'none','position':'absolute','top':0,'z-index':(opts.z_index-1),'height':height,'opacity':opts.opacity,'width':'100%'});$('<div/>').addClass('container_12').css({'margin':'0 auto','width':'960px','height':height,'background-image':'url('+opts.img_path+cols+'.png)','background-repeat':'repeat-y'}).appendTo(wrapper);$('<div>grid on</div>').appendTo($el).css({'position':'absolute','top':0,'left':0,'z-index':opts.z_index,'background':'#222','color':'#fff','padding':'3px 6px','width':'40px','text-align':'center'}).hover(function(){$(this).css("cursor","pointer");},function(){$(this).css("cursor","default");}).toggle(function(){$(this).text("grid off");$('#'+opts.grid_id).slideDown();},function(){$(this).text("grid on");$('#'+opts.grid_id).slideUp();});});};})(jQuery);
I am adding this simple plugin that I have working in a static html site, and am trying to add it to a wordpress development site based off of 960 gs. The jquery code links are correct but the console gives me this error "Uncaught TypeError: Cannot call method 'addGrid' of null" I got the code from this turtorial http://www.badlydrawntoy.com/2009/04/21/960gs-grid-overlay-a-jquery-plugin/
Here is the code I am using
/*<![CDATA[*/
// onload
$(function() {
$("body").addGrid(12, {img_path: 'img/'});
});
/*]]>*/
Here is the code for the plugin
/*
* @ description: Plugin to display 960.gs gridlines See http://960.gs/
* @author: badlyDrawnToy sharp / http://www.badlydrawntoy.com
* @license: Creative Commons License - ShareAlike http://creativecommons.org/licenses/by-sa/3.0/
* @version: 1.0 20th April 2009
*/
(function($){$.fn.addGrid=function(cols,options){var defaults={default_cols:12,z_index:999,img_path:'/images/',opacity:.6};var opts=$.extend(defaults,options);var cols=cols!=null&&(cols===12||cols===16)?cols:12;var cols=cols===opts.default_cols?'12_col':'16_col';return this.each(function(){var $el=$(this);var height=$el.height();var wrapper=$('<div id="'+opts.grid_id+'"/>').appendTo($el).css({'display':'none','position':'absolute','top':0,'z-index':(opts.z_index-1),'height':height,'opacity':opts.opacity,'width':'100%'});$('<div/>').addClass('container_12').css({'margin':'0 auto','width':'960px','height':height,'background-image':'url('+opts.img_path+cols+'.png)','background-repeat':'repeat-y'}).appendTo(wrapper);$('<div>grid on</div>').appendTo($el).css({'position':'absolute','top':0,'left':0,'z-index':opts.z_index,'background':'#222','color':'#fff','padding':'3px 6px','width':'40px','text-align':'center'}).hover(function(){$(this).css("cursor","pointer");},function(){$(this).css("cursor","default");}).toggle(function(){$(this).text("grid off");$('#'+opts.grid_id).slideDown();},function(){$(this).text("grid on");$('#'+opts.grid_id).slideUp();});});};})(jQuery);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定 WordPress 使用 Jquery.noConflict() 所以取决于你的调用方式jQuery 这可能是你的问题。尝试
I'm pretty sure that WordPress uses Jquery.noConflict() so depending on how you are calling jQuery this may be your problem. Try