简体   繁体   中英

Pass image url from variable to html in javascript

In my script, text(message.image) returns a dynamic URL path of an image. What would be the correct statement in a javascript?

This fails:

 $('<div/>').'<img src=\"'.text(message.image).'\">'.appendTo($('#msgDiv'));

I think you're missing () , + , append method and escaping " incorrectly, try with this:

$('<div/>').append('<img src="' + text(message.image) + '"/>').appendTo($('#msgDiv'));

Hope this helps,

The real answer is:

$('<div><img src="'+message.image+'"/></div>').appendTo($('#msgDiv'));

You have a couple syntactic errata in your code snippet:

  • You can't access a property with a string like you do.
  • Concatenation of strings is not with a dot but with a plus.
  • You are trying to execute text() on a string, not on the div.

I think you have a problem because you're using (double)quote badly

You escape the double quote but you're using simplequote. Try that :

$('<div/>').'<img src=\''.text(message.image).'\'>'.appendTo($('#msgDiv'));

And are you sure concerning the syntax $('').'SOME HTML CODE' ?

You are missing ( and ) there.

Also, you dont need to escape double quotes since you are using single quotes outside.

Try this:

$('<div/>').('<img src="'+text(message.image)+'">').appendTo($('#msgDiv'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM