JavaScript代码src问题
如果我将这段代码分开,将index.html到javascript.js脚本的所有代码分开,代码就不起作用 我编写了js文件的src地址,测试了带有警报的链接,但当我输入代码时,它不起作用 仅当它位于标记之间的index.html中时才有效。有人能帮我理解为什么吗?因为我需要分开,我不知道该怎么做 分离后我得到了这个 Index.html: JavaScript文件: 控制台告诉我未捕获的类型错误: 无法将属性“innerHTML”设置为nullJavaScript代码src问题,javascript,jquery,Javascript,Jquery,如果我将这段代码分开,将index.html到javascript.js脚本的所有代码分开,代码就不起作用 我编写了js文件的src地址,测试了带有警报的链接,但当我输入代码时,它不起作用 仅当它位于标记之间的index.html中时才有效。有人能帮我理解为什么吗?因为我需要分开,我不知道该怎么做 分离后我得到了这个 Index.html: JavaScript文件: 控制台告诉我未捕获的类型错误: 无法将属性“innerHTML”设置为null 也许您正在将引用外部js文件的脚本标记放在HEA
也许您正在将引用外部js文件的脚本标记放在HEAD部分?如果执行此操作,则当脚本完成加载和执行时,yout按钮不存在。如果是这种情况,要么将脚本引用放在文档末尾的同一位置,要么将代码包装在加载事件处理程序中。我发现了错误,我需要将脚本放在js文件中的函数中:
window.onload = function() {
}
我在构建DOM之前运行代码。我尝试在window.onload处理程序函数中运行我的代码,但见下面的注释,它成功了 查看你的浏览器控制台。你能编辑问题并包括所有源文件、html和js以及文件名吗?查看以前的文件无助于人们帮助你。分离后,他们需要查看所有多个文件。如果这太难了,试着在上面设置代码并发布链接。什么是外部文件名是itfunction.js或其他什么?它是function.js参考工作,我测试了一个警报
function init(){
sw=innerWidth;
sh=innerHeight;
$('#container').css('width',sw);
$('#container').css('height',sh);
}
$(document).ready(init)
var colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
function getRandomColor() {
return colors[Math.round(Math.random() * 5)];
}
function tango(layer) {
for(var n = 0; n < layer.getChildren().length; n++) {
var color = Kinetic.Util.getRGB(getRandomColor());
var shape = layer.getChildren()[n];
var stage = shape.getStage();
var radius = Math.random() * 100 + 20;
new Kinetic.Tween({
node: shape,
duration: 1,
x: Math.random() * stage.width(),
y: Math.random() * stage.height(),
rotation: Math.random() * 360,
radius: radius,
opacity: (radius - 20) / 100,
easing: Kinetic.Easings.EaseInOut,
fillRed: color.r,
fillGreen: color.g,
fillBlue: color.b
}).play();
}
}
var stage = new Kinetic.Stage({
container: 'container',
width: innerWidth,
height: innerHeight
});
var layer = new Kinetic.Layer();
for(var n = 0; n < 10; n++) {
var radius = Math.random() * 100 + 50;
var color = Kinetic.Util.getRGB(getRandomColor());
var shape = new Kinetic.RegularPolygon({
x: Math.random() * stage.getWidth(),
y: Math.random() * stage.getHeight(),
sides: Math.ceil(Math.random() * 5 + 3),
radius: radius,
fillRed: color.r,
fillGreen: color.g,
fillBlue: color.b,
opacity: (radius - 20) / 100,
draggable: true
});
layer.add(shape);
}
stage.add(layer);
document.getElementById('tango').addEventListener('click', function() {
tango(layer);
}, false);
window.onload = function() {
}