简体   繁体   English

javascript字符串中的引号+变量

[英]Quotes in javascript string +variable

I can't make it work, btn is a string here but 'licz' is a variable, I've tried anything, could someone help me and tell what is wrong with that ? 我无法使其工作,这里btn是一个字符串,但是'licz'是一个变量,我已经尝试了任何方法,有人可以帮助我并告诉我这是什么问题吗? Licz is a counter from 1 to 100, i want that line to give something like Licz是1到100的计数器,我希望该行给出类似

btn1, btn2 etc btn1,btn2等

  $('#slider').append("<div id='btn ' + \\'licz><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>"); 

Can someone show me the proper way to do this ? 有人可以告诉我正确的方法吗? I would be gratefull 我会很感激

    $('#slider').append("<div id='btn" + licz + "'><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

You would need to append it to your string like so: 您将需要将其附加到字符串中,如下所示:

 $('#slider').append("<div id='btn" + licz + "'><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

Or if you are using ES6 或者,如果您使用的是ES6

$('#slider').append(`<div id='btn${licz}'><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>`);

Your variable is not appended correctly it needs to be like this: 您的变量未正确附加,它需要像这样:

$('#test').append("<div id='btn '>" + licz + " <i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

HERE is an example of it working 这里是工作的一个例子

You need to stop and start the string appropriately where you want to. 您需要在需要的位置适当停止并启动字符串。 So you might instead like to try 因此,您可能想尝试

$('#slider').append("<div id='btn " + licz + "><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

Breaking the string properly with double quotes to allow you to insert the variable. 用双引号将字符串正确打断,以允许您插入变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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