html dom appendchild,javascript - HTML DOM appendChild - Stack Overflow

博客探讨了如何在HTML组件中动态展示数据。通过JavaScript函数`showColorLegendBox`,计算并追加颜色渐变盒的内容。在尝试更新HTML元素时遇到了`Failed to execute 'appendChild' on 'Node': The new child element is null`的错误。解决方案是使用jQuery的`empty()`方法清除现有内容,再用`append()`添加新的数据。同时,该博客还涉及到了地图热力图的生成和数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Basically I wanted to present dynamic data on HTML components. Here is my html:

And the script to get data and append the content to the html component:

function showColorLegendBox(max)

{

var content = "

Cases Severity

  • 0
  • " + Math.round(max/5*1) +"
  • " + Math.round(max/5*2) + "
  • " + Math.round(max/5*3) + "
  • " + Math.round(max/5*4) + "
  • " + max + "
";

var html = document.getElementById("colorGradiant");

html.appendChild(content);

}

However, by using these codes, it throw me an error message which is Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The new child element is null. I wonder is there any way to fix this?

Thanks in advance.

EDIT

function showColorLegendBox(max)

{

$('opacitydiv').empty();

var content = "

Cases Severity

  • 0
  • " + Math.round(max/5*1) +"
  • " + Math.round(max/5*2) + "
  • " + Math.round(max/5*3) + "
  • " + Math.round(max/5*4) + "
  • " + max + "

$("#opacitydiv").append(content);

//Hide the other elements of opacitydiv

document.getElementById('opacitytext').style.display = "none";

document.getElementById('opacityselect').style.display = "none";

document.getElementById('opacityslider').style.display = "none";

//Adjust the css for opacitydiv

document.getElementById('opacitydiv').style.display = "block";

document.getElementById("opacitydiv").style.width="25%";

}

function queryMHC()

{

if (heatmap_MHC) {

heatmap_MHC.setMap(null);

}

var visitAPI = "http://mhclivemap.appsolutrends.net/api/visits?" +

"start=" + $('#startDate').val() + "&end=" + $('#endDate').val() +

"&diagnosis=" + $('#mhc_type_select').val();

$.ajax({

url: "/main/Redirect.aspx?url=" + encodeURIComponent(visitAPI),

dataType: "json",

crossDomain:true,

success: function (res) {

var gradient = [

'rgba(185, 185, 203, 0)',

'rgba(145, 145, 192, 0)',

'rgba(65, 65, 207, 0)',

//dark blue

'rgba(30, 30, 229, 1)',

//light blue

'rgba(0, 185, 255, 1)',

'rgba(0, 255, 215, 1)',

//green

'rgba(0, 255, 15, 1)',

'rgba(0, 255, 0, 1)',

//yellow

'rgba(255, 255, 0, 1)',

'rgba(255, 235, 0, 1)',

//red

'rgba(255, 0, 0, 1)'

]

var result = res.visits;

var data = [];

var max = 0;

for (var i=0; i < result.length; i++) { // iterate thru each element in array

var count = result[i].count;

data.push({

location: new google.maps.LatLng(result[i].lat, result[i].lon),

weight: count

});

if (count > max) {

max = count;

}

}

heatmap_MHC = new google.maps.visualization.HeatmapLayer({

data: data,

radius: 30,

opacity: 0.8,

maxIntensity: max

});

heatmap_MHC.set('gradient', gradient);

heatmap_MHC.setMap(map);

showColorLegendBox(max);

},

error: function () {

alert('unable to load this layer, please try again later');

}

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值