我有一个运行jQuery的Chrome扩展程序,它在调用以下错误时可以很好地运行jQuery,但动态创建的元素除外: Refused to execute JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
有问题的代码的简化版本:
$('#add-dropdown-button').click(function() {
var newDropdown = '<select id="testDropdown"></select>';
$('td').append(newDropdown);
$('#newDropdown').append('<option value="1">Some Choice</option>');
});
该函数的前两行执行,并<select>
添加了元素,但最后一行引发错误,大概是因为<select>
从一开始就不存在,Chrome认为我正在尝试运行某种内联脚本。
这是错误还是我处理错误?
您的下拉菜单的ID是“ testDropdown”,而不是“ newDropdown”。尝试这个:
$('#add-dropdown-button').click(function() {
var newDropdown = '<select id="testDropdown"></select>';
$('td').append(newDropdown);
$('#testDropdown').append('<option value="1">Some Choice</option>');
});
或者简单地:
$('#add-dropdown-button').click(function() {
$('td').append('<select id="testDropdown"><option value="1">Some Choice</option></select>');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句