开发者

how to call this javascript function from inside another function?

pic_display+="<img src='"+pic_url+"' onClick='swapImage("+pic_url+");'>
开发者_StackOverflow社区

I am adding an image to the innerHTML of a div with the code above. I want to call the function swapImage(pic_url) whenever I click on the image.

I have still not figured out the syntaxes in javascript, always confuse them with php.

How should it be written ?

Thanks


You could dump the quoting issue by using a variable instead of the string:

... onclick="swapImage(this.src)" ...


Should be like this 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. For example.

  pic_display+="<img src='"+pic_url+"' onClick='javascript:swapImage(\""+pic_url+"\");'>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜