Javascript SCRIPT1014:无效的字符-引号符号

Javascript SCRIPT1014:无效的字符-引号符号,javascript,internet-explorer,invalid-characters,Javascript,Internet Explorer,Invalid Characters,我有一个问题: 数组[i]。idAuthor是一个字符串变量。我想把这个字符串传递给一个在追加字符串中调用的函数 除了Internet Explorer之外,该代码在Chrome和Firefox中运行良好。IE给我这个错误:SCRIPT1014:无效字符 我认为问题在于“-引用 我希望下面的例子有助于表达我的问题 <script> (...) $("#id").append("<div onClick='myFunc(`" + array[i].idAuthor + "

我有一个问题:

数组[i]。idAuthor是一个字符串变量。我想把这个字符串传递给一个在追加字符串中调用的函数

除了Internet Explorer之外,该代码在Chrome和Firefox中运行良好。IE给我这个错误:SCRIPT1014:无效字符

我认为问题在于“-引用

我希望下面的例子有助于表达我的问题

<script>
(...)
    $("#id").append("<div onClick='myFunc(`" + array[i].idAuthor + "`);'>" + i + "</div>");
(...)
<script>

(...)
$(“#id”).append(“+i+”);
(...)

有没有其他方法来处理我的情况,或者用另一个与IE兼容的字符替换“--引号?

您应该使用普通引号,但要对其进行转义,以便将其作为字符串的一部分进行解析:

$("#id").append("<div onClick='myFunc(\"" + array[i].idAuthor + "\");'>" + i + "</div>");
//------------------------------------^^   ----------------------^^
$(“#id”).append(“+i+”);
//------------------------------------^^   ----------------------^^

看起来您正在将反勾(`)放在字符串中

onClick='myFunc(`" + ... + "`);'>
在现代浏览器中,反勾号用于IE11不支持模板文本

相反,请尝试转义您的引用:

onClick='myFunc(\"" + array[i].idAuthor + "\");'>
//使用jquery创建元素
var elm=$('');
//将ID作为自定义属性
elm.attr('data-author-id',数组[i].idAuthor);
//为新元素添加一些html内容
html(i);
//点击它
elm.click(函数(){
//调用外部函数并将自定义标记属性作为值传递
myFunc($(this.attr('data-author-id'));
});
$(“#id”).append(elm);
这样的办法应该行得通

更多拍摄方式:

$("#id").append($('<div>')
.attr('data-author-id', array[i].idAuthor)
.html(i)
.click(function(){
    // call external function and pass your custom tag attribute as value
    myFunc( $(this).attr('data-author-id') );
}));
$(“#id”)。追加($('')
.attr('data-author-id',数组[i].idAuthor)
.html(i)
。单击(函数(){
//调用外部函数并将自定义标记属性作为值传递
myFunc($(this.attr('data-author-id'));
}));
jQuery有很多功能,可以控制标记属性、事件、值和很多有用的东西

$("#id").append($('<div>')
.attr('data-author-id', array[i].idAuthor)
.html(i)
.click(function(){
    // call external function and pass your custom tag attribute as value
    myFunc( $(this).attr('data-author-id') );
}));