[英]how to use jquery variable in inline css
i have an script that i want to generate i div where ever user clicks and that div stays there so now i have this jquery script 我有一个脚本,我想在用户单击的位置生成div,并且div停留在那里,所以现在我有了这个jquery脚本
$(document).ready(function() {
$('body').click(function(e) {
var offset = $(this).offset();
$('#x_axis').html(e.clientX - offset.left);
var x = (e.clientX - offset.left);
$('#y_axis').html(e.clientY - offset.top);
var y = (e.clientY - offset.top);
$('body').append('<div id="bloom" style="position: relative; left: HERE I WANT TO PLACE X;">asd</div>');
$('#bloom').css('left', x);
$('#bloom').css('top', y);
});
});
now on this line i want to add my X and Y variables to left and top attribute : 现在在这一行上,我想将我的X和Y变量添加到left和top属性:
$('body').append('<div id="bloom" style="position: relative; left: HERE I WANT TO PLACE X;">asd</div>');
any idea how this can be done ?? 任何想法如何做到这一点? or any better way to make this thing that when user click on each part of page a div generates there 或任何更好的方法来制作此东西,当用户单击页面的每个部分时,div都会在其中生成
You can use template literals for that 您可以为此使用模板文字
Solution 解
$('body').append(`<div id="bloom" style="position: relative; left: ${x};">asd</div>`);
you can concatenate the variable 您可以连接变量
eg: 例如:
$('body').append('<div id="bloom" style="position: relative; left:' + x + ';">asd</div>');
or 要么
use template literals 使用模板文字
$('body').append(`<div id="bloom" style="position: relative; left:${x};">asd</div>`);
您可以通过调整''
直接放置
$('body').append('<div id="bloom" style="position: relative; left:'+x+';">asd</div>');
您可以采用以下格式连接javascript变量:
$('body').append('<div id="bloom" style="position: relative; left:' + x + ';">asd</div>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.