Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量传递到javascript的样式css中_Javascript_Css - Fatal编程技术网

将变量传递到javascript的样式css中

将变量传递到javascript的样式css中,javascript,css,Javascript,Css,我试图使用JavaScript中的一个变量设置左边距。这可能吗?如何做到这一点 左边距必须是文本的clientWidth: var hoverBubble = document.getElementById(id); var linkWidth = link.clientWidth + 1; hoverBubble.style.margin-left = linkWidth + 'px'; 这是可能的,您只需要使用camelCase样式属性 hoverBubble.style.marginLe

我试图使用JavaScript中的一个变量设置左边距。这可能吗?如何做到这一点

左边距必须是文本的clientWidth:

var hoverBubble = document.getElementById(id);
var linkWidth = link.clientWidth + 1;
hoverBubble.style.margin-left = linkWidth + 'px';

这是可能的,您只需要使用camelCase样式属性

hoverBubble.style.marginLeft = linkWidth + 'px';

你不能像问题中那样使用连字符(减号)

否则,解释器将“认为”赋值的左侧有减法运算,这将导致语法错误


您可以在适当的调试工具上看到该错误,例如(在Chrome中使用F12并选择console)或(在Firefox上安装Firebug并按F12)。

有可能,您只需使用camelCase样式的属性即可

hoverBubble.style.marginLeft = linkWidth + 'px';

你不能像问题中那样使用连字符(减号)

否则,解释器将“认为”赋值的左侧有减法运算,这将导致语法错误

您可以在适当的调试工具上看到该错误,例如(在Chrome中使用F12并选择console)或(在Firefox上安装Firebug并按F12)。

试试这个

hoverBubble.style.marginLeft = linkWidth + 'px';
试试这个

hoverBubble.style.marginLeft = linkWidth + 'px';

您是否注意到浏览器开发人员控制台中的错误?您是否注意到浏览器开发人员控制台中的错误?