[英]jQuery String concatenation '+' error
I have a jQuery statement 我有一个jQuery语句
$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail('+data[i].email+')"></i>' );
Here if I click this icon then this function copyEmail()
is called which takes a string as an argument and adds it to the clipboard. 在这里,如果单击此图标,则会调用此函数
copyEmail()
该函数将字符串作为参数并将其添加到剪贴板。
But when I click on the icon I get an error in my console 但是,当我单击图标时,我的控制台出现错误
Uncaught SyntaxError: Invalid or unexpected token
未捕获的SyntaxError:无效或意外的令牌
then I tried doing it this way 然后我尝试这样做
$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail("'+data[i].email+'")"></i>' );
I got this error 我得到这个错误
Uncaught SyntaxError: Unexpected token }
未捕获的SyntaxError:意外令牌}
The problem is that I'm not able to pass a string in function. 问题是我无法在函数中传递字符串。
Can someone slove this for me ? 有人可以帮我吗?
尝试这个
$('.searchDisplay').append('<i class=\"ion-email\" onclick=\"copyEmail(\"'+data[i].email+'\")\"></i>' );
$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(\''+data[i].email+'\')"></i>' );
Problem in your code was... 您的代码中的问题是...
When you call a function with string as an argument you do someFunction('somestring');
当您使用字符串作为参数调用函数时,执行
someFunction('somestring');
, in your code you are missing the ''
wrapping the string. ,在您的代码中,您缺少包含换行符的
''
。
So you need to wrap data[i].email
with ' .. '
. 因此,您需要使用
' .. '
来包装data[i].email
。 I have wrapped the data[i].email
with ''
but as there was already one pair so I had to escape it with \\
. 我已经用
''
包裹了data[i].email
,但是由于已经有一对,所以我不得不用\\
对其进行转义。
You should use "\\" to avoid the errors, so you can make this: 您应该使用“ \\”来避免错误,因此可以这样做:
$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(\''+data[i].email+'\')"></i>' )
Edited 编辑
You do not need to wrap data[i].email
with '+ +'
at all in your function argument: 您完全不需要在函数参数
data[i].email
'+ +'
:
$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(data[i].email)"></i>' );
var data = [{'email': '123@gmail.com'}, {'email': 'xyz@gmail.com'}]; var i = 1; $('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(data[i].email)">Click To Test</i>' ); function copyEmail(email){ console.log(email); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="searchDisplay"></div>
$('.searchDisplay').append(`<i class="ion-email" onclick="copyEmail('${data[i].email}')"></i>`);
You're better to give it an id, or another identifying attribute, and then add the onClick handler after appending it, when it's available in the DOM, along the lines of: 最好给它一个id或另一个标识属性,然后在onDOM处理程序在DOM中可用的情况下,在附加它后添加onClick处理程序,方法如下:
$('.searchDisplay').append('<i id="num' + i + '" class...></i>');
$('#num' + i).on('click',
function () {
...
});
Or hook into all of them at once by selecting the .searchDisplay
class. 或通过选择
.searchDisplay
类来一次将它们挂钩。 You can use $(this)
in the function to determine which one was clicked. 您可以在函数中使用
$(this)
来确定单击了哪个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.