Javascript 使用 Gridster 添加网格时出错
Javascript error while adding grid using Gridster
$(function ($) {
var gridster;
widget_base_width: 240;
widget_base_height: 240;
widget_margin_h: 10;
widget_margin_v: 10;
gridActive: false;
this.gridster = $('.gridster ul').gridster({
widget_margins : [this.widget_margin_h, this.widget_margin_v],
widget_base_dimensions : [this.widget_base_width, this.widget_base_height],
helper : 'clone',
draggable : {}
}).data('gridster');
$(".chart-button").click(function () {
this.gridster.add_widget('<li id="hideil"><div id="test">heloooooo</div></li>');
});
});
我正在使用此代码通过 gridster 添加图块,但我收到错误提示:
Uncaught TypeError: Cannot read property 'add_widget' of undefined
(anonymous function)
x.event.dispatch jquery-1.10.2.min.js:22
v.handle jquery-1.10.2.min.js:22
单击处理程序的 this
对象与其外部的 this
对象不同。上下文不同:在处理程序中 this
对象指向 $('.chart-button')
。您可以在处理程序外部定义一个变量并使用它:
var gridster = $('.gridster ul').gridster(...).data('gridster');
$(".chart-button").click(function () {
gridster.add_widget('<li id="hideil"><div id="test">heloooooo</div></li>');
});
或者您可以将 this
(处理程序的覆盖上下文)绑定到处理程序:
this.gridster = $('.gridster ul').gridster(...).data('gridster');
$(".chart-button").click(function () {
gridster.add_widget('<li id="hideil"><div id="test">heloooooo</div></li>');
}.bind(this));
$(function ($) {
var gridster;
widget_base_width: 240;
widget_base_height: 240;
widget_margin_h: 10;
widget_margin_v: 10;
gridActive: false;
this.gridster = $('.gridster ul').gridster({
widget_margins : [this.widget_margin_h, this.widget_margin_v],
widget_base_dimensions : [this.widget_base_width, this.widget_base_height],
helper : 'clone',
draggable : {}
}).data('gridster');
$(".chart-button").click(function () {
this.gridster.add_widget('<li id="hideil"><div id="test">heloooooo</div></li>');
});
});
我正在使用此代码通过 gridster 添加图块,但我收到错误提示:
Uncaught TypeError: Cannot read property 'add_widget' of undefined
(anonymous function)
x.event.dispatch jquery-1.10.2.min.js:22
v.handle jquery-1.10.2.min.js:22
this
对象与其外部的 this
对象不同。上下文不同:在处理程序中 this
对象指向 $('.chart-button')
。您可以在处理程序外部定义一个变量并使用它:
var gridster = $('.gridster ul').gridster(...).data('gridster');
$(".chart-button").click(function () {
gridster.add_widget('<li id="hideil"><div id="test">heloooooo</div></li>');
});
或者您可以将 this
(处理程序的覆盖上下文)绑定到处理程序:
this.gridster = $('.gridster ul').gridster(...).data('gridster');
$(".chart-button").click(function () {
gridster.add_widget('<li id="hideil"><div id="test">heloooooo</div></li>');
}.bind(this));