如何将参数传递给'
包含的javascript函数
var name ="Lauren O'Donald";
var htmlAnch='<a onclick="javascript:selectEmployee(1100,"'+name+'");return false;"
href="javascript:void(0);">O'Donald, Lauren</a>';
$(document).append($(htmlAnch));
由于名称'Lauren O'Donald'
包含单引号,因此javascript函数未执行。
如何添加参数'
并准备动态html以使其起作用?
这是要生成的动态代码
var rows = new StringBuffer();
$(data).each(function(index) {
rows.append(String.format('<tr><td><a href="No.aspx"
onclick="javascript:selectEmployee({3},\"{1} {2}\");return
false;">{0}</a></td></tr>',
String.format("{0}, {1}", this.Surname, this.FirstName),
this.Surname,
this.FirstName,
this.Id
));
});
您可以通过在前面加上引号/字符\
来对其进行转义:
var string = 'my string with "double quotes" and \'single quotes\'';
var string = "my string with 'single quotes' and \"double quotes\"";
// ^ ^
使用动态字符串:
var foo = "bar with 'quotes'";
var string = 'my string with "double quotes" and ' + foo.replace(/'/g, "\\'");
//my string with "double quotes" and bar with \'quotes\'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句