在iPhone上设置innerHTML [英] Setting innerHTML on iPhone

查看:127
本文介绍了在iPhone上设置innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  document.getElementById('some_element')。innerHTML = newHTML; 

此功能在桌面和iPhone模拟器上的Web浏览器中正常工作。但在iPhone设备上,这随机运行良好。我的意思是,有时候工作,有时候不行。谷歌集团提出的一个解决方案是尝试重复该函数调用约20次,间隔50毫秒。来吧。关于那个神秘的延迟是否有一些合乎逻辑的解释?



以下是谷歌群组上的这个问题的链接:

http://groups.google.com/group/phonegap/browse_thread/thread/0d24a9fda0921407#



http://groups.google.com/group/phonegap/browse_thread/thread/a5787985293b6de1/8d826c8506377025?lnk=gst&q=innerHTML#8d826c8506377025

是代码被调用?



我猜如果这个代码被调用当文档是loadead时,就像onLoad()一样,DOM可能还没有完全加载。



对于这些类型的事情,我简单地包含了jQuery并做了这样的事情:

 < script type =text / javascriptsrc =jquery -1.4.3.min.js>< /脚本> 

< script type =text / javascript>
$(document).ready(function(){
$('#some_element')。html(newHTML);
});
< / script>

...或原型:

  document.observe('dom:loaded',function(){
// do yo thang ...
});

这可以确保DOM在脚本触发前完全加载;从而避免了这种竞争条件。

要包含jQuery,只需从它们的官方网站并将其放入您的Xcode项目中。或者,如果您的HTML文档是通过HTTP提供的,则可以将其放在服务器上的HTML文件旁边。



哦,在我忘记它之前。从项目加载我的HTML文件的代码如下所示:

  NSURL * fileUrl = [[NSBundle mainBundle] URLForResource:@ myHtmlFile
withExtension:@html];

NSURLRequest * request = [NSURLRequest requestWithURL:fileUrl];

[webView loadRequest:request];

希望有所帮助。


I am setting the innerHTML property of document element within this way:

document.getElementById('some_element').innerHTML = newHTML;

And this works fine in web browser on desktop and on iPhone simulator. But on iPhone device this works good randomly. I mean, sometimes works, sometimes does not. One proposed solution from google groups is to try to repeat this function call about 20 times with 50 ms pause between. Come on. Is there some logical explanation about that mysterious delay?

Here are the links for this issue on google groups:

http://groups.google.com/group/phonegap/browse_thread/thread/0d24a9fda0921407#

http://groups.google.com/group/phonegap/browse_thread/thread/a5787985293b6de1/8d826c8506377025?lnk=gst&q=innerHTML#8d826c8506377025

解决方案

When is that code called?

I guess if this code is called when the document is loadead, like onLoad(), the DOM might not have loaded completely yet.

Personally for these kind of things I simply include jQuery and do something like this:

<script type="text/javascript" src="jquery-1.4.3.min.js"></script>

<script type="text/javascript">
  $(document).ready( function() {
      $('#some_element').html(newHTML);
  } );
</script>

... or in Prototype:

document.observe('dom:loaded', function(){
    // do yo thang...
});

This ensures that the DOM is completely loaded before the script fires; thus avoiding this kind of race condition.

To include jQuery, just download it from their official website and drop it in your Xcode project. Alternatively, if your HTML document is served via HTTP, you can just drop it next to the HTML file on the server.

Oh, and before I forget it. The code that loads my HTML file from the project looks like this:

NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"myHtmlFile" 
                                         withExtension:@"html"];

NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];

[webView loadRequest:request];

Hope that helps.

这篇关于在iPhone上设置innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆