简体   繁体   English

如何从另一个函数内部调用此javascript函数?

[英]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.

相关问题 如何在另一个函数内调用javascript函数 - How to call a javascript function inside another function 如何在 javascript 中的另一个 function 中调用 function - How to call a function inside another function in javascript 如何在 JavaScript 中调用另一个 function 内部的 function - How to call a function inside of another function in JavaScript GWT-如何在另一个JavaScript函数中调用JavaScript函数 - GWT - How to call a JavaScript function inside another JavaScript function JavaScript:如何从原型属性上的另一个函数内部调用原型属性上的函数,都在闭包内部? - JavaScript: how to call a function on the prototype property, from inside another function on the prototype property, all inside a closure? 如何从另一个JavaScript调用JavaScript函数? - how to call a javascript function from another javascript? 如何调用另一个函数onsubmit表单中的javascript函数? - How to call javascript function inside another function onsubmit form? 如何在另一个JavaScript函数中调用变量或函数 - How to call a variable or function inside of another JavaScript function 如何在节点js中的另一个函数中调用javascript函数 - How to call a javascript function inside another function in node js 在JavaScript中如何在另一个功能块内调用一个函数? - in javascript how to call a function inside another function block?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM