我正在<div>
动态生成一个,我希望在生成时应将一个 jQuery 函数应用于它,但它不起作用。我尝试.on
了方法,但它没有按我想要的方式工作。<div>
我要应用该函数的对象是其父claas= portlet-content and id= live_graph
级<div>
的portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all'>
我哪里错了?
动态生成的<div>'s
HTMLstr += "<div class='portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all'>"+ \
"<div class='portlet-header ui-widget-header ui-corner-all'><span class='ui-icon ui-icon-minusthick'>"+\
"</span>Live Graph</div>" + \
"<div class='portlet-content' id='live_graph' style='height: 270px margin: 0 auto'>" + \
// Want to call the function when the above <div> is generated
"</div></div>"
return HttpResponse(HTMLstr)
jQuery函数(生成实时图表)
$(function () {
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
$('#live_graph').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
});
});
阿贾克斯代码
$.ajax({
type: "POST",
url: "/dashboard/",
data : { 'widgets_list' : widgets_list },
success: function(result){
console.log(widgets_list);
$("#column1").append(result); // div gets appended.
}
});
这是我尝试过的: -
代替
$(function () { in the jQuery function
我将其替换为:-
$('.portlet').on('ready', '#live_graph',function() {
但它没有用。我哪里出错了?
Question2 .on( events [, selector ] [, data ], handler(eventObject) )
语法on
可以ready
是活动吗?
这是我尝试过的
AJAX 成功函数
success: function(result){
console.log(widgets_list);
$("#column1").append(result);
$(".column").sortable("refresh");
$("#column1").generate_live_graph();
}
jQuery 函数
function generate_live_graph(){
$(function () {
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
$('#live_graph').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
});
});
}