javascript onClick 函数导致“Uncaught SyntaxError: Unexpected token }”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16704922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onClick function is causing "Uncaught SyntaxError: Unexpected token }" error
提问by William L.
I have a problem, I'm trying to dynamically add some html via javascript, and the html has a js function that's supposed to trigger when it's clicked, but I keep getting this error no matter what I do Uncaught SyntaxError: Unexpected token }
Has anyone had this problem before?
我有一个问题,我正在尝试通过 javascript 动态添加一些 html,并且 html 有一个 js 函数,应该在单击它时触发,但是无论我做什么,我都会收到此错误Uncaught SyntaxError: Unexpected token }
以前有人遇到过这个问题吗?
$("#addVehicle").click(function () {
vehicleQueue += '<li class="ui-state-default">' + $("#years option:selected").text() + ' - ' + $("#make option:selected").text() + ' - ' + $("#model option:selected").text() + ' - ' + $("#trim option:selected").text() + '<span><img onclick="javascript:alert("Hola");" class="removeVehicle" width="25px" src="http://haveagreatholiday.com/img/popup-closeButton.png"></span></li>';
$("#vehicleQueue").html('<ul id="sortable">' + vehicleQueue + '</ul>');
vehicleJson.push({
vehicleCount: vehicleCount,
vehicleYear: $("#years option:selected").text(),
vehicleMake: $("#make option:selected").text(),
vehicleModel: $("#model option:selected").text(),
vehicleTrim: $("#trim option:selected").text()
});
vehicleCount++;
});
This is my head:
这是我的头:
<head>
<script type="text/javascript" src="view.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="functions.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<style>
#sortable { list-style-type: none; margin: 0; padding: 0; width: 97%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.2em; height: 18px; }
#sortable li span {width: 16px;height: 17px;display: block;text-decoration: none;position: absolute;right: 18px;top: 3px;}
</style>
</head>
回答by Arun P Johny
Your onclick event handler has string escape problems, escape is as
您的 onclick 事件处理程序有字符串转义问题,转义为
onclick="alert(\'Hola\');"