如何用 JavaScript 设置元素的不透明度级别?
使用 JavaScript 中的 opacity 属性设置不透明度级别。你可以尝试运行以下代码,用 JavaScript 设置元素的不透明度级别 −
示例
<!DOCTYPE html> <html> <head> <style> #box { width: 450px; background-color: gray; } </style> </head> <body> <p>Click below to set Opacity.</p> <button type="button" onclick="display()">Set Opacity</button> <div id="box"> <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p> <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p> <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p> </div> <br> <script> function display() { document.getElementById("box").style.opacity = "0.5"; } </script> </body> </html>
广告