Javascript SRC 在 iPhone Safari 和 Android 中不起作用
根据要求,我需要动态更改图像 src,因此我使用 JavaScript 来完成此操作。
我正在使用 id 访问图像并根据用户点击添加新的 src,下面是我遇到问题的代码。在整个过程中,我得到了更新的图像 SRC 数据,但看起来很奇怪,我可以在桌面浏览器中看到更新的图像,但在 Mobile safari 和 Andriod 中看不到。
请帮忙
//This is id for fetching the user data
var text_food=document.getElementById("plateText_food");
//User Data
var currentFoodName=text_food.innerHTML;
//as per the requirement removing space and adding "-"
var regExp = /\s+/g;
var foodName=currentFoodName.replace(regExp,'-');
//Detail URL
var detailURL_food01 = 'img/beer_images_png/' + foodName + '.png';
//id FOR ACCESSING IMAGE
var updateFoodImage=document.getElementById("detailURL_food");
//adding updated src
updateFoodImage.src = detailURL_food01;
As per the requirement i need to change the image src dynamically, hence i using JavaScript to accomplish this.
I am accessing the image with id and adding new src depending on the user click, below is the code where i am getting the problem. Through out the process i am getting the updated SRC Data of image, but its looks strange that i could see updated image in desktop browser but not in Mobile safari and Andriod.
Please help
//This is id for fetching the user data
var text_food=document.getElementById("plateText_food");
//User Data
var currentFoodName=text_food.innerHTML;
//as per the requirement removing space and adding "-"
var regExp = /\s+/g;
var foodName=currentFoodName.replace(regExp,'-');
//Detail URL
var detailURL_food01 = 'img/beer_images_png/' + foodName + '.png';
//id FOR ACCESSING IMAGE
var updateFoodImage=document.getElementById("detailURL_food");
//adding updated src
updateFoodImage.src = detailURL_food01;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要在正确的时间运行,这个应该可以工作。这两种浏览器在渲染和加载内容时都遇到问题,速度要慢得多...在移动设备上工作的性质,这意味着您需要确定您的代码在 DOM 加载后运行,在 <例如,code>window.onload,否则当您执行
document.getElementById("plateText_food")
时...该id="plateText_food"
元素可能不会已经在 DOM 中准备好查找了。最后尝试一个简单的
alert()
来测试这一点(如果之前出错,您将不会收到警报,这是另一个指标):This should work, provided it's run at the right time. Both the browsers you're having issues when render and load content much slower...nature of working on mobile, that means you need to be sure that your code runs after the DOM is loaded, on
window.onload
for example, otherwise when you executedocument.getElementById("plateText_food")
...thatid="plateText_food"
element may not be in the DOM ready to find yet.Try a simple
alert()
to test this, at the end (you won't get the alert if it errors before, another indicator):