Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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向文本添加渐变_Javascript_Web_Linear Gradients_Radial Gradients_Gradient - Fatal编程技术网

如何使用JavaScript向文本添加渐变

如何使用JavaScript向文本添加渐变,javascript,web,linear-gradients,radial-gradients,gradient,Javascript,Web,Linear Gradients,Radial Gradients,Gradient,目前我正在做一个小项目,想知道如何使用JavaScript向文本添加渐变,就像在CSS中一样。使用JS向文本添加渐变与使用CSS添加渐变一样 根据写在上面的CSS,可以使用此代码添加JS const h1 = document.getElementsByTagName('h1')[0]; h1.style.background = 'linear-gradient(#eee, red)'; h1.style.webkitBackgroundClip = 'text'; h1.style.we

目前我正在做一个小项目,想知道如何使用JavaScript向文本添加渐变,就像在CSS中一样。

使用JS向文本添加渐变与使用CSS添加渐变一样

根据写在上面的CSS,可以使用此代码添加JS

const h1 = document.getElementsByTagName('h1')[0];
h1.style.background = 'linear-gradient(#eee, red)';
h1.style.webkitBackgroundClip = 'text';  
h1.style.webkitTextFillColor = 'transparent';

看看这里:你自己已经尝试过解决它了吗?