0

好的,现在我已经有了我需要用 Javascript 做的事情,但我不知道最好的方法!我将通过告诉你我“认为”它应该如何工作来解释......

  1. 在网页内部会有多个 span 标签,如下所示:<span onload="myfunction(1);"></span>

  2. myfunction() (外部托管)将等待页面完成加载并从每个 onload 事件中收集所有信息。

  3. 然后 myfunction() 会将这些信息发送到外部 php 页面进行处理。

  4. php 页面将返回一些 html。(可能在一个数组中,因为我想在 1 次调用中执行此操作)

  5. myfunction() 然后将 span 标签替换为 html。

步骤 3、4 和 5 我知道该怎么做,但步骤 1 和 2 我不知道如何实现?我在这里列出了我的所有步骤,以防有人看到我可能遇到的另一个大问题。

4

3 回答 3

1

因为只有<body>、<frame>、<frameset>、<iframe>、<img>支持onload事件,所以什么都不会发生。我建议您为每个跨度添加 id,并添加如下内容:

<body onload="collector.run()">
<span id="s1"></span>
<script> collector.delayFunction("s1",/data/) </script>
<span id="s2"></span>
<script> collector.delayFunction("s2",/data/) </script>
<span id="s3"></span>
<script> collector.delayFunction("s3",/data/) </script>
<span id="s4"></span>
<script> collector.delayFunction("s4",/data/) </script>
</body>

//function in js file somewhere above
var collector=(function (){
    this.stack={};

    this.delayFunction= function(id,data){
         this.collector.stack[id]=data;
    }

    this.run=function(){// this function will process all collected data
    }
})();
于 2009-08-06T15:31:37.400 回答
0

span 标签没有“onload”

于 2009-08-06T15:06:23.323 回答
0

您可以尝试使用类似于下一个的代码

function collector(){
  var spans = document.getElementsByTagName("SPAN");
  var commands = [];
  for (var i=0; i<spans.length; i++)
     if (spans[i].getAttribute("onload"))
        commands.push(spans[i].getAttribute("onload"));

   //here you have all onload commands in the array and can go to stage 3
}
if (window.addEventListener)
  window.addEventListener("load",collector,false):
else
  window.attachEVent("onload",collector);
于 2009-08-06T15:13:56.137 回答