简体   繁体   English

无法使用 Javascript 将 div 的背景设置为线性渐变

[英]Unable to set div's background to linear gradient using Javascript

I'm trying to make a dynamic div element with styles including linear gradient.我正在尝试使用 styles 制作一个动态div元素,包括线性渐变。 It should be displayed dynamically with a button, but the gradient does not get added.它应该使用按钮动态显示,但不会添加渐变。

 var x = document.getElementById("btn"); x.addEventListener("click", addbox); function addbox() { var y = document.createElement("Div"); y.id = "div1"; y.style.border = "1px"; y.style.borderRadius = "20px"; y.style.height = "200px"; y.style.width = "500px"; y.style.backgroundImage = "linearGradient(to bottom right, yellow, cyan)"; document.getElementsByTagName("body")[0].appendChild(y); }
 #btn { height: 50px; width: 150px; }
 <button id="btn" onclick="addbox()"> CLICK ME TO DISPLAY </button>

Your code has a lot of little mistakes but here is something that works:您的代码有很多小错误,但这是可行的:

<!doctype html>
<html>

<head>
    <title> ADD BOX ON CLICK FUNCTION </title>
    <style>
        #btn {
            height: 50px;
            width: 150px;
        }
    </style>
</head>

<body>
    <script>

        function addbox() {
            var y = document.createElement("Div");
            y.id = "div1";
            y.style.border = "1px";
            y.style.borderRadius = "20px";
            y.style.height = "200px";
            y.style.width = "500px";
            y.style.background = "linear-gradient(to bottom right, yellow , cyan)";
            document.getElementsByTagName("body")[0].appendChild(y);
        }

        window.addEventListener("DOMContentLoaded", (event) => {
            var x = document.getElementById("btn");
            x.addEventListener("click", addbox);
        });

    </script>
    <button id="btn"> CLICK ME TO DISPLAY </button>
</body>

</html>
  1. Your document.getElementById("btn") cannot work before the page has finished loading, so you have to put this inside a window.addEventListener("DOMContentLoaded") event that will fire only when your div is there您的document.getElementById("btn")在页面完成加载之前无法工作,因此您必须将其放在window.addEventListener("DOMContentLoaded")事件中,该事件仅在您的 div 存在时触发

  2. Remove the onclick="addbox()" from the div because it would fire twice per click从 div 中删除onclick="addbox()"因为每次点击它会触发两次

  3. linearGradient spelling is wrong. linearGradient拼写错误。 It's linear-gradient它是linear-gradient

  4. The property to apply the linear gradient is not y.style.backgroundImage but y.style.background应用线性渐变的属性不是y.style.backgroundImage而是y.style.background

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 中背景图像的线性渐变 - Linear gradient for background image in JavaScript 使用Javascript是否可以获取由线性或径向渐变呈现的背景图像? - Using Javascript is there a way to grab the background image rendered by a linear or radial gradient? 如何使用淡入效果更改线性渐变的主体背景 - How to change body's background for a linear gradient using a fade in effect 有没有办法设置 onClick 以使用 JavaScript/React 将 div 元素的背景更改为渐变? - Is there a way to set an onClick to change background of a div element to a gradient using JavaScript/React? 更改值时,HTML5 范围无法使用 javascript 将线性渐变设置为背景。 它会自动在样式中添加一个中心类 - HTML5 range cannot set linear-gradient as background using javascript when change value. It add automatically a center class into the style 如何使用 Javascript 设置线性渐变? - How can I set Linear Gradient using Javascript? jQuery设置背景:-webkit-linear-gradient - jQuery set background: -webkit-linear-gradient 如何使用线性渐变根据父级中的位置设置子 div 背景颜色 - How to set child div background color based on position in parent with a linear gradient 使用线性渐变的 Gatsby 中的多个背景图像 - Multiple background images in Gatsby using linear gradient 如何使用 Angular 中的自定义 CSS 类设置具有线性渐变的动态背景图像 URL - How to set dynamic background image URL with linear gradient using custom CSS class in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM