【发布时间】:2014-09-26 18:32:45
【问题描述】:
我遇到的问题是让我的外部 JavaScript 文件与我的 html 一起工作。
javascript 的重点是在提交为空的必填字段时高亮显示,然后在单击或 (onfocus) 时返回其原始颜色。
我不知道如何正确链接 javascript 以使其适用于 html
我尝试使用 onload="function();"在
中,但我知道这是不对的。我完全迷路了,所以任何帮助或指导将不胜感激。此处的 HTML:http://pastebin.com/Zwsqn7kr
这里的 CSS:http://pastebin.com/zpNkpALd
这里是 JavaScript:
window.onload = function () {
document.getElementById('mainForm').onsubmit = function (e) {
var t = document.getElementsById("title");
var d = document.getElementsById("description");
var l = document.getElementsById("license");
var tv = document.getElementsById("title").value;
var dv = document.getElementsById("description").value;
if (tv == null || tv === "") {
e.preventDefault();
title.style.backgroundColor = "red";
title.parentNode.style.backgroundColor = "red";
}
if (dv == null || dv === "") {
e.preventDefault();
description.style.backgroundColor = "red";
description.parentNode.style.backgroundColor = "red";
}
if (!l.checked) {
e.preventDefault();
license.style.backgroundColor = "red";
license.parentNode.style.backgroundColor = "red";
}
t.onfocus = function () {
title.style.backgroundColor = "white";
title.parentNode.style.backgroundColor = "white";
};
d.onfocus = function () {
description.style.backgroundColor = "white";
description.parentNode.style.backgroundColor = "white";
};
l.onfocus = function () {
license.style.backgroundColor = "EBF4FB";
license.parentNode.style.backgroundColor = "EBF4FB";
};
};
};
【问题讨论】:
-
它在做什么或不做什么? “将无法工作” 不可调试。
-
如果有的话,我认为 javascript 没有正确链接到 html
-
控制台显示什么错误。
-
Uncaught SyntaxError: Unexpected token ) html 的第 13 行。它指的是 onload="function();"我添加的部分。我知道这是不正确的,因为当我删除它时我没有错误
标签: javascript html