在html中不显示内容是,html()之后的div内容未显示

我遇到了ajax调用和.html()的问题,正如您可以在下面的javascript中看到我使用geoloc回调进行ajax调用并刷新div的内容。

div的id可以是" result-shops-ajax-desktop"或"结果商店-ajax-mobile"在我的例子中,php变量$ type =" desktop"

问题是尽管使用了.html()

,但不会重新加载div的内容

我的ajax调用正在工作,因为msg.response得到了我想要的内容。 .html()似乎有效,因为警报显示了msg.response的内容,但我无法在页面上看到它:/

另一件事是奇怪的,当我直接放入div的id时,显示的内容,但是当我回应$ type时,它不起作用,尽管html产生的是好的(div的id是& #34;结果商店-Ajax的桌面"。)

我希望我用自己糟糕的英语清楚地表达自己,我真的很感激这个奇怪问题的任何帮助。

谢谢!

这是我的php文件中的javascript:

jQuery.noConflict();

jQuery(document).ready(function($){

function geolocShow(position) {

$.ajax({

type: "POST",

url: "<?php echo $this->getUrl('shops/index/checkavailability'); ?>",

dataType : "json",

data: "sku=<?php echo $product->getSku(); ?>&lat="+position.coords.latitude+"&lng="+position.coords.longitude,

success: function(msg){

$("#result-shops-ajax-<?php echo trim($type); ?>").html(msg.response);

alert($("#result-shops-ajax-<?php echo trim($type); ?>").html());

}

});

}

if(navigator.geolocation) {

navigator.geolocation.getCurrentPosition(geolocShow);

}

});

生成的html:

jQuery.noConflict();

jQuery(document).ready(function($){

function geolocShow(position) {

$.ajax({

type: "POST",

url: "http://localhost/shops/index/checkavailability/",

dataType : "json",

data: "sku=abc&lat="+position.coords.latitude+"&lng="+position.coords.longitude,

success: function(msg){

$("#result-shops-ajax-desktop").html(msg.response);

alert($("#result-shops-ajax-desktop").html());

}

});

}

if(navigator.geolocation) {

navigator.geolocation.getCurrentPosition(geolocShow);

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你将上面的代码放在 HTML 文件,但是没有看到图像,可能是因为你没有等待 ECharts 库加载完成。你可以尝试将 JavaScript 代码放在 `window.onload` 的回调函数,确保 JavaScript 代码在 HTML 元素加载完成之后再执行。以下是代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts Demo</title> <!-- 引入 ECharts --> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.9.0/echarts.min.js"></script> </head> <body> <div id="main" style="width: 600px;height:400px;"></div> <script> window.onload = function () { // 初始化 ECharts 实例 var myChart = echarts.init(document.getElementById('main')); // 定义横坐标时间数组和纵坐标数据数组 var dataTime = []; var dataValue = []; // 定义随机生成数据函数 function randomData() { return Math.round(Math.random() * 1000); } // 动态更新数据 setInterval(function () { // 获取当前时间 var now = new Date(); // 将时间加入时间数组 dataTime.push(now.toLocaleTimeString().replace(/^\D*/,'')); // 将随机生成的数据加入数据数组 dataValue.push(randomData()); // 限制数据数组最大长度为 20 if (dataTime.length > 20) { dataTime.shift(); dataValue.shift(); } // 更新图表 myChart.setOption({ title: { text: '动态数据', left: 'center' }, tooltip: { trigger: 'axis', axisPointer: { animation: false } }, xAxis: { type: 'category', boundaryGap: false, data: dataTime }, yAxis: { type: 'value', boundaryGap: [0, '100%'] }, series: [{ name: '数据', type: 'line', data: dataValue, animation: false, smooth: true, areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(255, 158, 68, 0.1)' }, { offset: 1, color: 'rgba(255, 158, 68, 0.5)' }]) }, lineStyle: { color: 'rgb(255, 70, 131)' }, itemStyle: { color: 'rgb(255, 70, 131)' } }] }); }, 1000); }; </script> </body> </html> ``` 以上代码会在 HTML 文件加载完成之后,在 `window.onload` 函数初始化 ECharts 实例和绑定数据,确保了 JavaScript 代码在 HTML 元素加载完成之后再执行,从而避免了找不到 HTML 元素的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值