简体   繁体   English

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

[英]Javascript SRC is not working in IPhone Safari and Android

As per the requirement i need to change the image src dynamically, hence i using JavaScript to accomplish this. 根据要求,我需要动态更改图像src,因此我使用JavaScript来完成此操作。

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. 我正在访问具有ID的图像并根据用户单击添加新的src,以下是我遇到问题的代码。 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. 在整个过程中,我获取了图像的更新的SRC数据,但是看起来奇怪的是,我可以在桌面浏览器中看到更新的图像,而在Mobile Safari和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;

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. 两种浏览器在渲染和加载内容时都遇到问题,速度慢得多...在移动设备上工作的特性,这意味着您需要确保代码在DOM加载后运行,例如在window.onload上运行,否则您执行document.getElementById("plateText_food") ... id="plateText_food"元素可能尚未在DOM中准备就绪。

Try a simple alert() to test this, at the end (you won't get the alert if it errors before, another indicator): 最后,尝试一个简单的alert()进行测试(如果之前发生错误,您将不会再得到另一个指示器的警报):

alert(detailURL_food01);
updateFoodImage.src = detailURL_food01;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM