Javascript SRC 在 iPhone Safari 和 Android 中不起作用

发布于 2024-10-05 08:04:02 字数 732 浏览 6 评论 0原文

根据要求,我需要动态更改图像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦行僧 2024-10-12 08:04:03

只要在正确的时间运行,这个应该可以工作。这两种浏览器在渲染和加载内容时都遇到问题,速度要慢得多...在移动设备上工作的性质,这意味着您需要确定您的代码在 DOM 加载后运行,在 <例如,code>window.onload,否则当您执行document.getElementById("plateText_food")时...该id="plateText_food"元素可能不会已经在 DOM 中准备好查找了。

最后尝试一个简单的 alert() 来测试这一点(如果之前出错,您将不会收到警报,这是另一个指标):

alert(detailURL_food01);
updateFoodImage.src = detailURL_food01;

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 execute document.getElementById("plateText_food")...that id="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):

alert(detailURL_food01);
updateFoodImage.src = detailURL_food01;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文