【问题标题】:external javascript will not work外部 javascript 将不起作用
【发布时间】: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


【解决方案1】:

好吧,如果您将document.getElementsById 替换为document.getElementById,它将开始突出显示错误的字段。

【讨论】:

    【解决方案2】:

    尝试将链接到脚本的行放在body 末尾之前。

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 2015-03-29
      • 2017-03-22
      相关资源
      最近更新 更多