[英]how to call this javascript function from inside another function?
pic_display+="<img src='"+pic_url+"' onClick='swapImage("+pic_url+");'>
I am adding an image to the innerHTML of a div with the code above. 我正在使用上面的代码将图像添加到div的innerHTML中。 I want to call the function
swapImage(pic_url)
whenever I click on the image. 我想在每次单击图像时调用函数
swapImage(pic_url)
。
I have still not figured out the syntaxes in javascript, always confuse them with php. 我仍然没有弄清楚javascript中的语法,总是将它们与php混淆。
How should it be written ? 应该怎么写?
Thanks 谢谢
您可以使用变量而不是字符串来转储引用问题:
... onclick="swapImage(this.src)" ...
Should be like this onClick='swapImage(\\""+pic_url+"\\");'
应该像这样
onClick='swapImage(\\""+pic_url+"\\");'
Note you need to quote the pic URL when it is the argument since you are meaning to use a string and not a variable. 请注意,当图片网址作为参数时,您需要引用它,因为您的意思是使用字符串而不是变量。
Instead of 代替
onClick='swapImage("+pic_url+");'>
try 尝试
onClick='swapImage(\""+pic_url+"\");'>
You need to wrap the function argument in quotes like you have with the src attribute. 您需要像使用src属性一样将函数参数包装在引号中。 For example.
例如。
pic_display+="<img src='"+pic_url+"' onClick='javascript:swapImage(\""+pic_url+"\");'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.