下面感觉有点过度设计,有直接访问器吗?
const bg = window.getComputedStyle(document.querySelector('.source')).backgroundColor
document.querySelector('.result').innerHTML =
bg.startsWith('rgba') ? bg.replace(/rgba\(\d+\, \d+\, \d+\, (\d(\.\d+)?)\)/g, '$1') : 1
.source {
background-color: rgba(0, 0, 255, 0.44);
}
<div class="source" style="width: 20px; height: 20px;"></div>
Alpha:
<div class="result"></div>