我已经创建了带有onclick事件的动态<a />
标记,并且此事件调用了一个包含对象作为参数的函数,我通过了该函数但是在链接上单击发生错误 SyntaxError:expected expression,got脚本结束
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<script>
function callconfirm(cb,pmessage)
{
// var isConfirmed
BootstrapDialog.confirm({
title: 'WARNING',
message: pmessage,
type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
closable: false, // <-- Default value is false
draggable: false, // <-- Default value is false
btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
callback:cb /*function(result) {
// result will be true if button was click, while it will be false if users close the dialog directly.
if(result) {
return true;
}else {
return false;
}
}*/
});
}
function b1checkConfirm(result,id)
{
//callconfirm();
console.log("B1 click and result is "+result);
console.log(this.p1);
console.log(this.p2);
}
</script>
</head>
<body>
<script type="text/javascript">
document.write('<a href="javascript:void(0)" onClick="javascript:callconfirm(b1checkConfirm.bind({p1: Hello, p2:World}),"Hello")" >Click</a>');
</script>
</body>
答案 0 :(得分:1)
您需要逃避"
:
<script type="text/javascript">
document.write('<a href="javascript:void(0)" onClick="javascript:callconfirm(b1checkConfirm.bind({p1: Hello, p2:World}),\"Hello\")" >Click</a>');
</script>
因为代码会认为onClick事件在你的第二个"
之后完成
答案 1 :(得分:0)
您的问题是document.write的这一部分:
b1checkConfirm.bind({p1: Hello, p2:World}),"Hello"
Hello 和 World 必须进行转义,因为它们是HTML的一部分,您可能会写:
b1checkConfirm.bind({p1: 'Hello', p2: 'World'}),'Hello'
摘录:
function callconfirm(cb,pmessage)
{
// var isConfirmed
BootstrapDialog.confirm({
title: 'WARNING',
message: pmessage,
type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
closable: false, // <-- Default value is false
draggable: false, // <-- Default value is false
btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
callback:cb /*function(result) {
// result will be true if button was click, while it will be false if users close the dialog directly.
if(result) {
return true;
}else {
return false;
}
}*/
});
}
function b1checkConfirm(result,id)
{
//callconfirm();
console.log("B1 click and result is "+result);
console.log(this.p1);
console.log(this.p2);
}
document.write('<a href="javascript:void(0)" onClick="javascript:callconfirm(b1checkConfirm.bind({p1: 'Hello', p2: 'World'}),'Hello')" >Click</a>')
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>