[英]Enter param in inline javascript function
How can I add a param to the removeSound
function in this JavaScript code? 如何在此JavaScript代码中向
removeSound
函数添加一个参数?
I cannot get it to work. 我无法让它发挥作用。
var param = "111";
coder = '<a href="javascript:;" onClick="javascript: removeSound(' + param + ');" >Delete</a>'
Update 更新
I do not know why code = was removed! 我不知道为什么代码=被删除了! This is needed to explain the problem context.
这是解释问题背景所必需的。
You need to use different quotes. 您需要使用不同的引号。 Try something like this ...
尝试这样的事......
<a href="javascript:;" onClick="javascript: removeSound('PARAM HERE');" >Delete</a>
I just want to add a proper double quoting escape method to the answers as none showed a correct way to escape double quotes inside of an onClick
attribute. 我只是想在答案中添加一个正确的双引号转义方法,因为没有人在
onClick
属性中显示正确的方法来转义双引号。
In HTML it is not sufficient to use only a backslash when escaping a JavaScript double quote. 在HTML中,在转义JavaScript双引号时仅使用反斜杠是不够的。 You need the proper HTML entity which is
"
您需要正确的HTML实体,即
"
This is a live example: 这是一个实例:
<a href="#" onClick="removeSound("121212");">Delete</a>
However for readability I would recommend using Antony Scott's way by using single quotes. 但是为了便于阅读,我建议使用单引号使用Antony Scott的方式。
<a href="#" onClick="removeSound('121212');">Delete</a>
To add the param as a variable from whatever script your HTML is generated in you should do this: 要将param作为变量添加到您生成HTML的任何脚本中,您应该这样做:
code = '<a href="javascript:;" onClick="javascript: removeSound("' + the_param + '");" >Delete</a>';
The other possible way should be: 另一种可能的方式应该是:
code = '<a href="javascript:;" onClick="javascript: removeSound(\'' + the_param + '\');" >Delete</a>';
试试这个
code = '<a href="#" onclick="removeSound("PARAM HERE");">Delete</a>';
试试这个
<a href="#" onClick="removeSound('121212')" >Delete</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.