Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 插入、延迟、原型和附加元素_Javascript_Html_Dynamic_Prototypejs - Fatal编程技术网

Javascript 插入、延迟、原型和附加元素

Javascript 插入、延迟、原型和附加元素,javascript,html,dynamic,prototypejs,Javascript,Html,Dynamic,Prototypejs,这可能是一个更一般的浏览器/javascript问题,而不是一个特定于原型的问题,但我认为最好在这里提问,因为你们都倾向于真正理解javascript和浏览器的大量细节。这就来了 如果我执行以下代码: HTML: Javascript: $('area').insert({bottom: "<div id="inserted"></div>"}); var count = 0; var f = function() { if ($('inserted') =

这可能是一个更一般的浏览器/javascript问题,而不是一个特定于原型的问题,但我认为最好在这里提问,因为你们都倾向于真正理解javascript和浏览器的大量细节。这就来了

如果我执行以下代码:

HTML:

Javascript:

$('area').insert({bottom: "<div id="inserted"></div>"});

var count = 0;
var f = function() {
      if ($('inserted') == null) {
            console.log("not there");
            count++;
            if (count > 50) {
                  $('area'.insert({bottom: "<div id="inserted"></div>"});
                  count = 0;
            }
            f.defer();
      } else {
        console.log("there");
     }
};

f();
但有时它会这样做

not there
not there
not there
there
我这样假设是因为插入是排队的,然后浏览器在其下一个事件循环中将节点插入DOM。我知道webkit是一个单线程的,所以有时候它不在那里,然后它到达那里是有道理的,所以我想我必须等到它在那里,然后才能在插入的节点上做“下一件事”。firefox和IE呢?它们都是单线程的吗?铬会发生什么

有时,我也会看到以下情况发生,这与我非常相关:

not there
not there
... 50 times
not there
there
这种情况在webkit(macos)和iphonewebkit上经常发生,我可以很容易地复制它。我已经构建了一些简单的代码来实现这一点,但所有这些对我来说似乎有点疯狂,因为当我查看其他代码时,他们甚至没有考虑到这一点。在将HTML文本插入DOM元素时,它们从不允许DOM元素显示

任何答案/建议都会非常有用


Kiran

我想Fabien的问题是正确的:只需等待DOM加载<代码>文档。观察(“dom:loaded”,f)

所有这些都是在dom:loaded之后运行的,所以问题不是dom没有加载,而是有时即使加载了dom,内容也不会总是显示出来。在深入研究之后,我发现这似乎发生在iphone safari浏览器上,而不是桌面版本上。这可能只是时间问题。感谢您抽出时间回答这些问题

Firefox和其他浏览器都是单线程的。您应该这样声明您的函数:函数f(){…代码中出现语法错误是正常的吗?DOM就绪时是否执行f()?document.observe(“DOM:loaded”,f);
not there
not there
... 50 times
not there
there