내가 함수 만들기 위해 노력하고 있습니다와 클래스의 CSS 값을 인쇄 :읽기 및 순수 자바 스크립트
- 가 특정 데이터 요소와 요소를 그랩을, 즉 "데이터 findDeclaration = 요소" 이 요소에서
- 는, 클래스 그것은이 클래스에 또 다른 특정 데이터 요소 (즉, 데이터 writeDeclaration = "요소") 모든 클래스 '규칙/값 내부
- 표시를 임명 모든 CSS 값을 읽어
- 읽습니다.
HTML :
<span class="slap0" data-finddeclaration="element">The great white fox</span> <div class="description" data-writedeclaration="element"> </div> <span class="slap1" data-finddeclaration="element">The great white fox</span> <div class="description" data-writedeclaration="element"> </div> <span class="slap2" data-finddeclaration="element">The great white fox</span> <div class="description" data-writedeclaration="element"> </div> <span class="slap3" data-finddeclaration="element">The great white fox</span> <div class="description" data-writedeclaration="element"> </div>
JS :
function readOutCssRules() { var dataElement = document.querySelectorAll('[data-finddeclaration="element"]'); var classRules = document.styleSheets[0].rules || document.styleSheets[0].cssRules; var writeDataElement = document.querySelectorAll('[data-writedeclaration="element"]'); for (var i = 0; i < dataElement.length; i++) { dataElement[i].className; if (classRules[i].selectorText == "." + dataElement[i].className) { console.log(classRules[i].cssText); writeDataElement[i].innerHTML = classRules[i].cssText; } } } readOutCssRules();
그러나 예상대로 기능이 작동하지 않는 내가 지금까지 나는 여기 어디에
입니다. 아이디어가 있으십니까? 미리 감사드립니다.
을 받고? –
클래스의 내용을 가져 오지만 html 구조에 요소를 더 추가하면 해당 요소가 표시되지 않습니다. 게다가, 해당 div 안에 올바르게 표시된 클래스 이름이 아닌 규칙을 볼 필요가 있습니다. jQuery를 사용할 수 없습니다. 그렇지 않으면 문제가 많지 않을 것입니다. – Sidius
CSS == 클래스 인 이유는 무엇입니까? – HudsonPH