javascript - 为什么这个函数在 javascript 中抛出 'strict' 错误?

标签 javascript jquery css drag-and-drop

我想要一个在页面加载时调用的函数,然后该函数会将监听器添加到页面上的 div 标记,监听器会将我的 javascript 文件中的全局变量更改为 true 或 false,但是我无法执行此操作,因为当我尝试执行此操作时收到关于函数严格的错误。

浏览器:Chrome

错误:

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

查看代码:

<script>
$(document).ready(function () {
    var imgs = document.querySelectorAll(".cut-images");
    [].forEach.call(imgs, function (img) {
        img.addEventListener("dragstart", dragStart, false);
        img.addEventListener("dragend", dragEnd, false);
    });
});

Javascript:

    var isOnDiv = false;

function dragStart(e) {
    this.style.opacity = "0.4";

        document.getElementById("chuckImage").addEventListener("mouseenter", function () { isOnDiv = true; });
        document.getElementById("chuckImage").addEventListener("mouseout", function () { isOnDiv = false; });



    e.dataTransfer.effectAllowed = "move";
}

    function dragEnd(e) {

        if (isOnDiv === true)
            alert("ding");
        this.style.opacity = "1.0";
    }

有谁知道为什么我的函数会抛出这个错误?

最佳答案

在你的 js 文件中全局使用了如下几行

 'use strict'; 

由于这个原因,有些东西违反了严格模式并引发了错误。

可以看到更多详情here...

关于javascript - 为什么这个函数在 javascript 中抛出 'strict' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52501547/

相关文章:

javascript - 将 nodetext 附加到 svg 元素

javascript - React Native <{}> 语法

javascript - 网格中的 Page_validators 用于客户端验证

javascript - 具有单独切换的多个显示/隐藏 div

javascript - 检测 iPad Safari 或 Zoom

javascript - CSS 规则取决于内容

javascript - 需要优化的解决方案,并且不使用 jquery 函数

javascript - Javascript SetTimeOut 会影响页面性能吗?

javascript - jQuery 查找标签之间的文本

javascript - raphael js Pan 功能使用 raphael-pan-zoom