如何为通过setAttribute设置的playbut属性应用display:none样式?
为自定义属性
playbut应用display:none的几种方法 方法1:使用CSS属性选择器(推荐)
直接通过CSS选择器匹配带有playbut属性的videoblock元素,设置隐藏样式。将这段CSS加入页面的<style>标签或外部样式文件即可:
/* 匹配所有带有playbut属性的videoblock元素 */ videoblock[playbut] { display: none; } /* 更精准:仅匹配playbut属性值为特定内容的元素(比如response.button是某个固定值时) */ videoblock[playbut="目标值"] { display: none; }
方法2:通过JavaScript动态设置行内样式
在设置完playbut属性后,直接给元素添加隐藏样式,也可根据response.button的具体值做条件判断:
getvalue(response) { const videobyte = document.createElement('videoblock'); videobyte.setAttribute('background', response.background); videobyte.setAttribute('playbut', response.button); // 只要设置了playbut就隐藏 videobyte.style.display = 'none'; // 或者仅当response.button满足特定条件时隐藏(比如值为"hidden") // if (response.button === 'hidden') { // videobyte.style.display = 'none'; // } }
方法3:通过类名配合CSS控制
如果后续有更多样式需求,可给元素添加类名,再用CSS统一管理:
getvalue(response) { const videobyte = document.createElement('videoblock'); videobyte.setAttribute('background', response.background); videobyte.setAttribute('playbut', response.button); // 添加自定义类名 videobyte.classList.add('playbut-hidden'); }
对应的CSS:
.playbut-hidden { display: none; }
内容的提问来源于stack exchange,提问作者Terry C
相关产品推荐
相关产品推荐

