如何在 javascript 中修复此错误“缺少;声明之前”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4458932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:29:22  来源:igfitidea点击:

How can I fix this error "missing; before statement" in javascript?

javascriptdebuggingfirebug

提问by faressoft

How can I fix this error "missing; before statement" in javascript ?

如何在 javascript 中修复此错误“缺少;声明之前”?

alt text

替代文字

My HTML Page : http://etrokny.faressoft.com

我的 HTML 页面:http: //etrokny.faressoft.com

My Javascript Code : http://etrokny.faressoft.com/javascript.php

我的 Javascript 代码:http: //etrokny.faressoft.com/javascript.php

采纳答案by Tom van der Woerdt

When assigning a function to a variable, you need a semicolon after the function.

将函数赋值给变量时,函数后需要一个分号。

Example: var func = function() { return false; };

例子: var func = function() { return false; };

回答by Jordi

Put a semicolon after all statements. JavaScript does it automatically for you when you "forget" one at the end of a line**, but since you used a tool to make everything fit on one line, this doesn't happen anymore.

在所有语句后放一个分号。当您“忘记”一行末尾的内容时,JavaScript 会自动为您执行此操作**,但是由于您使用了一种工具将所有内容都放在一行中,因此这种情况不会再发生了。

** it should also be happening when a statement is followed by a }, but it's just bad practice to rely on it. I would always write all semicolons myself.

** 当语句后跟 a 时也应该发生这种情况},但依赖它是不好的做法。我总是自己写所有的分号。

Actually, you know what, since it's so easy, I did it for you:

事实上,你知道吗,既然它如此简单,我为你做了:

function getSelected() {
    var selText;
    var iframeWindow = window;
    if (iframeWindow.getSelection) {
        selText = iframeWindow.getSelection() + "";
    } else if (iframeWindow.document.selection) {
        selText = iframeWindow.document.selection.createRange().text;
    }
    selText = $.trim(selText);
    if (selText != "") {
        return selText;
    } else {
        return null;
    }
}
$(document).ready(function () {
    function scan_selectedText() {
        if (getSelected() == null) {
            return false;

        }
        if (getSelected().length < 25) {
            return false;
        }
        $(document)[0].oncontextmenu = function () {
            return false;
        };
        var result = true;
        var selected_Text = getSelected();
        selected_Text = selected_Text.replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
        $('#content .para').each(function () {
            var accepted_text = $.trim($(this).text());
            accepted_text = accepted_text.replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
            if (accepted_text.search(selected_Text) > -1) {
                result = false;
            }
        });
        var AllAccepted = "";
        $('#content .para').each(function () {
            var correntDiv = $.trim($(this).text()).replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
            AllAccepted = AllAccepted + correntDiv + " ";
        });
        if ($.trim(AllAccepted).search(selected_Text) > -1) {
            return false;
        }
        if (!result) {
            return false;
        }
        var body = $.trim($('body').text());
        body = body.replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' ');
        var bodyWithoutDivs = body;
        $('#content').each(function () {
            var correntDiv = new RegExp($.trim($(this).text()).replace(/ {2,}/g, ' ').replace(/\s{2,}/g, ' '), "");
            bodyWithoutDivs = bodyWithoutDivs.replace(correntDiv, '');
        });
        if (bodyWithoutDivs.search(selected_Text) > -1) {
            return false;
        }
        if (body == selected_Text) {
            return true;
        }
        return true;
    }
    $(document).mousedown(function (key) {
        if (key.button == 2) {
            if (scan_selectedText() == true) {
                $(document)[0].oncontextmenu = function () {
                    return false;
                };
            } else {
                $(document)[0].oncontextmenu = function () {
                    return true;
                };
            }
        }
    });
    var isCtrl = false;
    $(document).keyup(function (e) {
        if (e.which == 17) isCtrl = false;
    }).keydown(function (e) {
        if (e.which == 17) isCtrl = true;
        if (e.which == 67 && isCtrl == true) {
            $("#content2").animate({
                opacity: 0
            }, 500).animate({
                opacity: 1
            }, 500);
            if (scan_selectedText() == true) {
                return false;
            } else {
                return true;
            }
        }
    });
    document.onkeypress = function (evt) {
        if (evt.ctrlKey == true && evt.keyCode == 67) {
            $("#content2").animate({
                opacity: 0
            }, 500).animate({
                opacity: 1
            }, 500);
            if (scan_selectedText() == true) {
                return false;
            } else {
                return true;
            }
        }
    };
    $('*').bind('copy', function (key) {
        if (scan_selectedText() == true) {
            return false;
        } else {
            return true;
        }
    });
});

回答by snarky

First thing, start with the raw human-written version of your javascript, you know, the unminimized non-machine-generated version

首先,从你的 javascript 的原始人工编写版本开始,你知道,未最小化的非机器生成版本

After you've fixed you've made sure it is free from syntax errors .... and you minimize it, don't make a mistake when copy/pasting

修复后,您已确保它没有语法错误.... 并将其最小化,复制/粘贴时不要出错