gpt4 book ai didi

javascript - 如何将参数放入 vanilla JS 中的 setAttribute 函数

转载 作者:行者123 更新时间:2023-11-30 14:30:34 24 4
gpt4 key购买 nike

我有一些像下面这样的函数,它为元素设置 inline css 属性。这是一种非常简化的 JQuery .css() 方法:

function cssSetAttr(styleName, value) {
for (var i = 0, n = someObject.length; i < n; i++) {
someObject[i].setAttribute('style', [styleName][value]);
}
someObject.cssSetAttr('color', 'red');

问题是如何像这样将参数放入 setAttribute 中:

setAttribute('style', 'styleName: value') 

最佳答案

您可以使用 template literals

setAttribute('style', `${styleName}: ${value}`);

关于javascript - 如何将参数放入 vanilla JS 中的 setAttribute 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51263239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com