简体   繁体   English

试图将两块预制的 javascript 代码 function 一起制作

[英]Trying to make two pieces of premade javascript code function together

I don't know anything about javascript but I can't get these two scripts to work together, if anyone could fix my mistakes I'd be very grateful.我对 javascript 一无所知,但我无法让这两个脚本一起工作,如果有人能纠正我的错误,我将不胜感激。 The scripts are premade from http://www.mf2fm.com/rv/ and function just fine on their own.这些脚本是由http://www.mf2fm.com/rv/和 function 自行制作的。 I've tried to solve the issue myself but I'm unfortunately entirely clueless.我试图自己解决这个问题,但不幸的是我完全一无所知。 I got this error when I tried to test running what I pasted below on a test site;当我尝试在测试站点上测试运行下面粘贴的内容时出现此错误; "JavaScript error: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. on line 48" Line 48 being: document.body.appendChild(stars[j]); “JavaScript 错误:未捕获的 TypeError:无法在 'Node' 上执行 'appendChild':参数 1 不是 'Node' 类型。在第 48 行” 第 48 行是:document.body.appendChild(stars[j]); if that's any help.如果这有任何帮助。 The problem seems to be localized to these lines, though they run fine without the second script.问题似乎局限于这些行,尽管它们在没有第二个脚本的情况下运行良好。

addLoadEvent(clicksplode);

function clicksplode() { if (document.getElementById) {
  var i, j;
  window.onscroll=set_scroll;
  window.onresize=set_width;
  document.onclick=eksplode;
  set_width();
  set_scroll();
  for (i=0; i<bangs; i++) for (j=sparks*i; j<sparks+sparks*i; j++) {
    stars[j]=createDiv('*', 13);
    document.body.appendChild(stars[j]);
  }
}}

Well, for one thing, you're copying a script that dates back to 2002 at the very latest.好吧,一方面,您正在复制最迟可以追溯到 2002 年的脚本。 These belong in a museum.这些属于博物馆。 I had a whole nostalgic flashback to the 90s loading up that site from your source code.从您的源代码加载该站点时,我怀念着 90 年代的回忆。

The error you're getting is that value of stars[j] is not a DOM Node, so appendChild() doesn't know what to do with it.您得到的错误是stars[j]的值不是 DOM 节点,因此 appendChild() 不知道如何处理它。

Since it seems like you're already familiar with the dev console, I'd suggest putting a breakpoint in at that line 48 where you see your error and seeing what is actually being passed to appendChild().由于您似乎已经熟悉开发控制台,因此我建议您在第 48 行放置一个断点,您可以在其中看到错误并查看实际传递给 appendChild() 的内容。 Then you can walk up the stack trace to see where that bad value is coming from.然后你可以沿着堆栈跟踪走,看看那个坏值是从哪里来的。 You can also, in chrome dev tools, turn on "pause on exceptions" to automatically stop your code in the debugger when an error is thrown.您还可以在 chrome 开发工具中打开“异常暂停”以在引发错误时自动停止调试器中的代码。 It's the icon of a pause button in a circle in the upper right of the source tab in the chrome debugger.它是 chrome 调试器中源选项卡右上角圆圈中的暂停按钮图标。

Best of luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM