将参数传递给javascript onclick函数 [英] passing parameter to javascript onclick function
问题描述
我遇到从javascript onClick函数获取参数的问题
I am having problem with getting parameter from javascript onClick function
title = "as"
$('<li onClick=pushRight('+hello+') class="item"></li>')
和我的控制台日志打印此Uncaught SyntaxError:意外的令牌}
and my console log print this Uncaught SyntaxError: Unexpected token }
推荐答案
title = "as"
$('<li onClick="pushRight("'+hello+'")" class="item"></li>')
查看添加的双引号。 '+ hello +'
还要在 pushRight
函数上加双引号。
See the added double quotes. "'+hello+'"
Also double quotes over your pushRight
function.
您的代码将无效,因为它会呈现无效的html,如下所示。
Your code won't work because it render an invalid html as shown below.
<li onClick=pushRight(as) class="item"></li>
看看,pushRight之前和之后都没有报价,也是之前和在你的
字符串sa
之后。知道错误并纠正。不建议在html中使用内联js。您必须在这些场景中绑定事件。
See, there were no quotes before and after pushRight, also before and after your string
"sa"
. Know the mistake and correct. Using inline js with html is not recommended.You have to bind events in these scenarios.
这篇关于将参数传递给javascript onclick函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!