最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Interpolation in JavaScript - Stack Overflow

programmeradmin5浏览0评论

I have this jQuery code

$("#selector").html('<a href=url>text</a>');

where url and text are JavaScript variables. How would I evaluate them inside quotes?

I have this jQuery code

$("#selector").html('<a href=url>text</a>');

where url and text are JavaScript variables. How would I evaluate them inside quotes?

Share Improve this question edited Mar 1, 2014 at 1:42 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 11, 2011 at 0:14 Justin MeltzerJustin Meltzer 13.6k34 gold badges119 silver badges182 bronze badges 1
  • I created an open source project for interpolation you can try. github./zsong/Kiwi Thank you. – zs2020 Commented Feb 15, 2013 at 18:27
Add a ment  | 

3 Answers 3

Reset to default 11

You just concatenate the strings together with +:

$('#selector').html('<a href='+url+'>'+text+'</a>');

While @kingjiv's answer is absolutely right, if you'll be doing a lot of templating with jQuery it might be worth checking out the tmpl* plugin to help keep things organized.

*note: This plugin never made it past beta, and is no longer being maintained, the link above is for archival purposes only.

For anyone landing here post 2015: use ES6 template literals. These are string literals, enclosed in backticks, which allow embedded expressions.

$('#selector').html(`<a href='${url}'>${text}</a>`);
发布评论

评论列表(0)

  1. 暂无评论