4 回答

TA貢獻1786條經驗 獲得超11個贊
let class_name = document.querySelector('[data-name="header"]').getAttribute("class"); //this will get the class of attr data-name
document.querySelector('.' + class_name).style.display = 'none'; //this will set it's display to none;
//You can directly also do so ↓↓--
//document.querySelector('[data-name="header"]').style.display = 'none';
<div class="_69x2sms" data-name="header">
<div class="_18tvv4h"><a target="_blank" href=""><span>Done<span class="_qx8n3fw">..!</span></span></a></div>
<div class="_8sfqbvd">have fun</div>
</div>

TA貢獻1784條經驗 獲得超9個贊
看來您對問題的解決方案可能不是解決問題的最佳方法。如果你想用 css 修改動態元素,你可以為它們創建一個靜態類:
.custom_class {
display: none;
}
然后將類附加到某些元素element.classList.add("custom_class");而不是嘗試修改現有類。

TA貢獻1829條經驗 獲得超7個贊
好吧,您可以像編輯其他<style />元素一樣編輯 - 元素的內容。至少在任何中等現代的瀏覽器中(我相信> IE9)。
例如:
// Your JS
var prop= document.querySelectorAll('[data-name="header"]')[0].getAttribute("class");
var class_name= "." + prop ;
// New JS
var style = document.getElementsByTagName('style')[0];
style.textContent += class_name + ' { display: none; }'

TA貢獻1797條經驗 獲得超6個贊
你不能在已經構建的“STYLE”標簽中附加一些東西,但你可以像這樣創建一個:
var css = class_name +'{ display: none }',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
head.appendChild(style);
style.type = 'text/css';
if (style.styleSheet){
// This is required for IE8 and below.
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
添加回答
舉報