在javascript中,如何获取setattribute值来引用变量?

时间:2018-05-21 17:44:26

标签: javascript setattribute

const main = document.querySelector('main');

const n = '40px';

const div = document.createElement('div');
div.setAttribute('class', 'grid');

main.appendChild(div);

div.setAttribute('style', 'background-color: #f9f9f9; width: n; height: n'); 

我希望n引用上面40px的值。最终n将是用户输入。 (虽然我不知道如何将数字转换为字符串+ px ......但这又是另一个问题。)

0 个答案:

没有答案