-1

我对此感到困惑。我正在使用 WordPress 网站,我有 2 个简单的 Javascript 警报功能。我在图像的 onclick 中使用其中一个来确保脚本有效。如果我的脚本中没有任何空行,一切都很好,但是如果我添加任何空行,则没有任何效果(请参阅下面的代码以获取示例)。

Javascript 和 PHP 都忽略空格。我唯一不确定的是HTML。关于 HTML 如何处理空白的内容并不多(尽管我觉得我在某处读到,如果它不在元素中,它会忽略它)。HTML 空格是否敏感(除了在元素中)?如果不是,为什么空行会弄乱我的脚本?我想这没什么大不了的,因为我可以删除空行,但我想让我的代码更整洁并找出导致这种情况的原因。

这是我的代码示例:

// This works fine because there's no empty lines
<script type='text/javascript'>
        function showAlert() {
            alert("The first showAlert function works!");
        }
        function showAnotherAlert("The other alert is working!");
</script>

// This won't do anything because of the empty lines between the functions
<script type='text/javascript'>
        function showAlert() {
            alert("The first showAlert function works!");
        }

        function showAnotherAlert() {
        alert("The second alert is working!");
        }
</script>

有人对此有任何见解吗?

4

1 回答 1

1

您的代码应编写如下;

<script type='text/javascript'>
    function showAlert() {
        alert("The first showAlert function works!");
    }

    function showAnotherAlert(text) {
        alert(text);
    }
</script>

使用Javascriptfunction showAnotherAlert("The other alert is working!");无法识别的语法,因为它的格式无效。如果您希望将文本动态传递给警报,只需通过上面示例中所示的函数传递它。

于 2013-04-27T05:45:40.510 回答