[英]IE8 appendChild “Unexpected call to method or property access”
I really need some help with this issue I'm having. 对于这个问题,我确实需要一些帮助。 Only seem to have on IE8. 似乎只有在IE8上。 IE7 works fine which is weird. IE7正常工作,这很奇怪。 I'm using IE9 (F2) development tool IE8 emulator which seem to work good. 我正在使用看起来不错的IE9(F2)开发工具IE8仿真器。 I also checked on VMware native IE8 and getting the same results. 我还检查了VMware本机IE8并获得了相同的结果。 Anyway is that a issue with appendChild in IE8? 无论如何,这是IE8中appendChild的问题? How can I solve this issue and is there a jquery or better way of coding this section. 我如何解决此问题,是否有jquery或更好的方法来编码此部分。
IE report this as the issue ** s.appendChild(document.createTextNode(jcode)); IE将其报告为问题** s.appendChild(document.createTextNode(jcode)); ** "Unexpeced call to method or property access" **“对方法或属性访问的意外调用”
for (var i=0; i<showContain.length+1; i++)
{
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
var jcode = "$('#showdiv" + i + "').mouseover(function(){$('.showcaseoff').hide(); $('#showcase" + i + "').show()});";
s.appendChild(document.createTextNode(jcode));
document.body.appendChild(s);
}
Thanks... 谢谢...
This does seem to simply be a limitation in some versions of IE. 在某些版本的IE中,这似乎只是一个限制。 But, you seem open to alternatives: 但是,您似乎可以选择:
How can I rearrange this to work? 我该如何重新安排工作?
I'm guessing this snippet came from frustration that i
seemed to change value for the .show()
towards the end. 我猜想这个片段来自沮丧, i
似乎最终改变了.show()
值。 That would be due to JavaScript's current lack of block-level scoping and mixing in the asynchronous nature of .mouseover()
, so it would reference i
after the loop had already run to completion and incremented it to showContain.length+1
. 那是由于JavaScript目前缺乏.mouseover()
异步特性的块级作用域和混合,因此它将在循环运行完成后引用i
并将其递增到showContain.length+1
。
Eventually, you'll be able to use let
in place of var
to create a block-level i
within the loop (the keyword is part of the upcoming ECMAScript 6 standard). 最终,您将能够使用let
代替var
在循环内创建块级i
(关键字是即将到来的ECMAScript 6标准的一部分)。 But, you can currently use a closure to isolate each value of i
in its own function
scope. 但是,您当前可以使用闭包将i
每个值隔离在其自己的function
范围内。
function eachShowcase(i) {
$('#showdiv' + i).mouseover(function(){
$('.showcaseoff').hide();
$('#showcase' + i).show()
});
}
for (var i=0; i<showContain.length+1; i++) {
eachShowcase(i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.