Javascript var in string

后端 未结 3 1210
日久生厌
日久生厌 2021-01-25 01:49

So I have this code and would like to know how I could go ahead to put my javascript var into this string. I cannot seem to make a working code for myself.

For the image

相关标签:
3条回答
  • 2021-01-25 02:15

    You cannot use " & ' this together unless you escape it with a \.

     var text = '<img src="' + picture.value + '">'
    

    OR

     var text = "<img src=\"" + "Hi" + "\">"
    
    0 讨论(0)
  • 2021-01-25 02:15

    Assuming your picture.value is not empty.

    var text = '<img src=' + picture.value + '>';
    

    Example: https://jsfiddle.net/no293etL/

    0 讨论(0)
  • 2021-01-25 02:29

    Try using ES6 new feature called Template literals (using quote). It is more cleaner and simpler.

    var picture = {'value':'test-src-location'};
    var text = `<img src="${ picture.value }">`;
    console.log(text);

    0 讨论(0)
提交回复
热议问题