本文介绍了onClick函数导致“Uncaught SyntaxError:Unexpected token}”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,我正在尝试通过javascript动态添加一些html,并且html有一个js函数,它应该在点击时触发,但无论我做什么我都会收到此错误未捕获的SyntaxError:意外的令牌}
以前是否有人遇到此问题?
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?
$("#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++;
});
这是我的头:
<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>
推荐答案
你的onclick事件处理程序有字符串转义问题,转义是as
Your onclick event handler has string escape problems, escape is as
onclick="alert(\'Hola\');"
这篇关于onClick函数导致“Uncaught SyntaxError:Unexpected token}”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!