jQuery:Javascript抛出错误“;操作不安全”;设置值时

jQuery: Javascript throws Error "The operation is insecure" when setting value

本文关键字:不安全 操作 设置 Javascript 出错 错误 jQuery      更新时间:2023-09-26

我正试图为那些不解释输入元素占位符属性的浏览器找到一个后备解决方案。我有这个简单的jQuery脚本,但它抛出了一个错误

SecurityError: "The operation is insecure.
this.value = val;"

这是我的脚本:

$('document').ready(function(){
       $('input').each(function() {
           if ($(this).val() === '' || $(this).val() === undefined) {
               $(this).val($(this).attr('placeholder'));
           }
       });
});

有人知道我能做什么吗?或者我做错了什么?或者这个错误意味着什么?它发生在Firefox中,还没有在其他浏览器中测试过。

我刚刚修复了项目中的一个类似问题。事实证明,我正试图设置<input type="file" ...>输入的值。您可能会面临同样的问题,因为您要选择文档的所有输入,而不管它们的类型如何。

如果您安装了firebug,请在尝试修改输入值之前插入log命令,尝试查找导致此错误的输入。

$('document').ready(function(){
       $('input').each(function() {
           if ($(this).val() === '' || $(this).val() === undefined) {
               // Log goes here
               window.console.log(
                   'There is an input without a value!', 
                   this);
               $(this).val($(this).attr('placeholder'));
           }
       });
});

我在其他函数中收到了一个不安全的警告。原因很简单,调用库函数时将数组作为参数,但它需要一个元素(dom)。结果是意料之中的,但我确信,无论如何都不会这样。因此,请检查您的变量类型是否是您(或另一方)想要的类型。