我有一个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我认为我试图运行某种内联脚本。
这是一个错误还是我接近错了?
答案 0 :(得分:0)
您的下拉列表的ID是&#39; testDropdown&#39;,而不是&#39; newDropdown&#39;。试试这个:
$('#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>');
});