iPhone Webkit jQuery怪异:无法更新内容 [英] iPhone Webkit jQuery strangeness: Can't update content
问题描述
使用jQuery和我的WebViewController在iPhone上遇到最奇怪的问题.
Running into the strangest problem on iPhone using jQuery with my WebViewController.
我在其中添加内容的div:
I have a div into which I append content:
<div id="thumbnails">
Here are your thumbnails:
<div id="mythumbs"></div>
</div>
代码如下:
for (var i = 0; i < thumbs.length; i++) {
var item = thumbs[i];
$('<img class="imgthumb" />').data('url', item.Url).attr({
"src": item.Thumbnail.Url,
}).appendTo($('#mythumbs'));
};
效果很好.
然后我切换到其他div($('#thumbnails').hide(); $('#someotherdiv').show()
),继续从事我的业务,最后切换回缩略图div.
Then I switch to other divs ($('#thumbnails').hide(); $('#someotherdiv').show()
), go on about my business, and eventually switch back to the thumbnails div.
在这一点上,我似乎无法再使用jQuery将任何内容附加到该div.我可以删除就好了,但是附加项不再起作用.
At this point I can't seem to append any content to that div using jQuery anymore. I can remove just fine, but the append no longer works.
完全相同的代码在iPhone之外的Firefox和Safari上也能很好地工作,但是一旦在嵌入式WebKit中,它就会失败.
The exact same code works great on Firefox and Safari outside of the iPhone, but once in the embedded WebKit it fails.
如果我直接使用JavaScript修改DOM,则可以使用,但是如果使用jQuery,则失败:
If I modify the DOM directly using JavaScript it works, but if I use jQuery it fails:
var x = document.createTextNode('THE FIRST THING');
document.getElementById('thumbspage').appendChild(x);
$('#thumbspage').append('-- THE SECOND THING');
出现第一件事",但没有出现.
"THE FIRST THING" shows up but "THE SECOND THING" doesn't.
有什么想法吗?这东西让我发疯.
Any ideas? This thing is driving me nuts.
推荐答案
最终找到了解决方法:
我正在使用window.location调用我的Objective-c函数.原来,如果我在对window.location的调用周围添加了setTimeout,一切将再次开始工作.我猜想调用window.location会立即调用Objective-C代码,从而中断了javascript线程,并使事情处于不良状态.通过添加setTimeout,它可以使javascript线程在跳转到Objective-C代码之前完成.或类似的东西.
I was calling my objective-c function using window.location . It turned out if I added a setTimeout around the call to window.location everything started working again. I'm guessing calling window.location would immediately call the objective-C code, interrupting the javascript thread, leaving things in a bad state. By adding the setTimeout it allows the javascript thread to finish before jumping into the objective-C code. Or something like that.
这篇关于iPhone Webkit jQuery怪异:无法更新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!