SCRIPT1014: Invalid character - Quote symbol

a 夏天 提交于 2019-12-07 10:31:23

问题


I have this problem:

array[i].idAuthor is a String variable. I want to pass this String to a function which is called inside an append-String.

The code works fine in Chrome and Firefox except for Internet Explorer. IE gives me this error: SCRIPT1014: Invalid character

I think the issue are the `-Quotes.

I hope the following example helps to express my problem.

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

Is there another way to handle my situation or to replace the `-Quotes with another character that is compatible with IE?


回答1:


It looks like you're putting backticks (`) into your string there.

onClick='myFunc(`" + ... + "`);'>

In modern browsers, backticks are used for template literals. IE11 doesn't support template literals.

Instead, try escaping your quotes:

onClick='myFunc(\"" + array[i].idAuthor + "\");'>



回答2:


You should use normal quotes, but escape them so they are parsed as part of the string:

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



回答3:


//create element using jquery
var elm = $('<div>');

//put ID as custom attribute
elm.attr('data-author-id', array[i].idAuthor);

//put some html content for new element
elm.html(i);

// catch click on it
elm.click(function(){
    // call external function and pass your custom tag attribute as value
    myFunc( $(this).attr('data-author-id') );
});

    $("#id").append(elm);

something like that should work.

of more shot way:

$("#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') );
}));

jQuery have lot of functionality control tag attributes, events, values and lot's of useful stuff.



来源:https://stackoverflow.com/questions/37332381/script1014-invalid-character-quote-symbol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!