通过单引号传递参数
如何使用'
included '
将参数传递给javascript函数
var name ="Lauren O'Donald"; var htmlAnch='O'Donald, Lauren'; $(document).append($(htmlAnch));
由于名称'Lauren O'Donald'
包含单引号,因此javascript函数未执行。
如何添加参数'
和准备动态HTML以使其工作?
这是要生成的动态代码
var rows = new StringBuffer(); $(data).each(function(index) { rows.append(String.format('{0} ', 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\'
您可以使用\
来转义它:
var htmlAnch='O\'Donald, Lauren';
但是,当您使用jQuery标记此问题时,更好的解决方案是将事件连接到元素并使用data-*
属性来存储相关信息,这将避免使用丑陋的onX
属性。 试试这个:
var $htmlAnch = $('' { text: "O'Donald, Lauren" , data-id: 1100, data-name: "Lauren O'Donald" }).click(function(e) { e.preventDefault(); selectEmployee($(this).data('id'), $(this).data('name')); }); $(document).append($htmlAnch);
编写自己的函数以返回转义字符串。 演示
将您的字符串作为参数传递给此函数,您将获得转义字符串。 如果要转义更多字符,还可以向阻止列表中添加更多字符
function remove_quotes(values1) { var values = values1.toString(); var str = ""; var blockList = ['"','\'','\\']; // This is the list of key words to be escaped var flag = 0; for(var i = 0;i
尝试这样的事情
var htmlAnch='O\'Donald, Lauren';
在.aspx文件中,您可以执行以下操作:
')**">