2012-09-06 4 views
0

Javascript/jQuery를 사용하면 속성 이름을 값과 함께 HTML 속성 이름과 함께 가져올 수 있습니까? 문서. 그들은이 관계없이 인 경우 : 연결된 임베디드주어진 요소에 대한 CSS 속성 및 HTML 속성을 각각 가져옵니다.

인라인 스타일

<h1 style="font-weight="900">Heading 1</h1> 

<style> 
h1 
{ 
font-weight: bold; 
} 
</style> 

<link href="main.css" rel="stylesheet" type="text/css" media="all" /> 

@import url("reset.css"); 

그리고 수입에 관계없이 위의

  • 버전에 의해 적용되는

    • 사용자 에이전트/브라우저 (IE, FF, GC, AS, 영업 이익)
    • 기본 스타일의 위의

    글쎄, Firebug 나 개발자 도구 i n FF, 그리고 다른 UA의 비슷한 도구들도 있지만 몇 가지 능력이 부족합니다. 요소가 왼쪽에 표시되고 위의 모두가 오른쪽에 표시되는 jQuery 플러그인 유형을 찾고있었습니다 (iframe에?).

    간단히 말하면 하나의 요소만으로 문서를 만들고 내 브라우저의 왼쪽에 표시하고 위의 내용을 오른쪽에 표시합니다.

  • +3

    가능한 복제본 [jQuery를 사용하여 모든 요소 CSS 속성의 목록을 얻는 방법은 무엇입니까?] (http :// /stackoverflow.com/questions/1471118/how-can-i-get-list-of-all-element-css-attributes-with-jquery) – Josh

    +0

    @ Josh :이 질문과 100 개가 넘는 질문이 있지만 나는 "플러그인"보다는 acutal 코드. Javascript의 Noob/jQuery입니다. – Jawad

    +0

    @ 자 와드 : [this] (http://stackoverflow.com/a/6416477/420001) 링크가 있습니다. – Josh

    답변

    3

    당신은 문서 개체의 getComputedStyle() 메소드 요소의 속성 필드 사용할 수 있습니다 :이 요소가있는 각 CSS 스타일 필드 객체를 반환해야

    var oDiv = document.getElementById("div1"); 
    var css = document.defaultView.getComputedStyle(oDiv, null); 
    var attr = oDiv.attributes; 
    

    합니다. 그런 다음 DOM의 모든 요소를 ​​반복하는 간단한, 깊이 우선 트리 거리를 쓸 수 있습니다 (I가 쉽게 따라 할 수 있도록 jQuery로 쓴) : 나는 카운터를 넣어

    var stack = new Array(); 
    stack.push($('html')[0]); 
    var i = 0; 
    while(stack.length > 0 && i < 100){ 
        //pop the next element off the stack 
        var ele = stack.pop(); 
    
        var css = document.defaultView.getComputedStyle(ele, null); 
        var attr = ele.attributes; 
    
        //do things with the css object 
        console.log(css); 
    
        //do things with the attributes 
        console.log(attr); 
    
        //add children to the stack 
        $(ele).children().each(function(index, child){ 
         stack.push(child); 
        }); 
        i++; 
    } 
    

    주 (I)를 사용하여 반복 횟수를 100으로 제한하고 페이지에 많은 요소가있는 경우 브라우저가 끊어지지 않도록하십시오. 원한다면 제거 할 수 있지만 조심하십시오. 또한 검색 루트는 DOM의 모든 노드가 될 수 있지만 html 태그로 시작했습니다.

    귀하의 의견을 바탕으로, 이것을 구현하는 방법을 살펴 보겠습니다. CSS/애트리뷰트 객체를 콘솔에 출력하는 것 뿐이므로 실제로 원하는 부분을 수정해야합니다.

    스크립트 :

    <!DOCTYPE html> 
    <html> 
    <head> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
    function doStuff(){ 
        var stack = new Array(); 
        stack.push($('html')[0]); 
        var i = 0; 
        while(stack.length > 0 && i < 100){ 
        //pop the next element off the stack 
        var ele = stack.pop(); 
    
        var css = document.defaultView.getComputedStyle(ele, null); 
        var attr = ele.attributes; 
    
        //do things with the css object 
        console.log(css); 
    
        //do things with the attributes 
        console.log(attr); 
    
        //add children to the stack 
        $(ele).children().each(function(index, child){ 
         stack.push(child); 
        }); 
        i++; 
        }   
    } 
    </script> 
    </head> 
    <body> 
        <button type="button" onclick="doStuff()">Click Me!</button> 
    </body> 
    </html> 
    

    나는 것

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 
    <script type="text/javascript"> 
    function doStuff(){ 
        var stack = new Array(); 
        stack.push($('html')[0]); 
        var i = 0; 
        while(stack.length > 0 && i < 100){ 
        //pop the next element off the stack 
        var ele = stack.pop(); 
    
        var css = document.defaultView.getComputedStyle(ele, null); 
        var attr = ele.attributes; 
    
        //do things with the css object 
        console.log(css); 
    
        //do things with the attributes 
        console.log(attr); 
    
        //add children to the stack 
        $(ele).children().each(function(index, child){ 
         stack.push(child); 
        }); 
        i++; 
        }   
    } 
    </script> 
    

    HTML 버튼 그것을

    <button type="button" onclick="doStuff()">Click Me!</button> 
    

    전체 구현을 실행 당신이 이걸로 성취하려는 것을 듣고 싶습니다. 이것은 느린 작업이며 페이지에 넣은 태그를 검사하는 것이 일반적으로별로 도움이되지 않습니다 ...

    +0

    나는이 모든 코드를 가지고 있지만 구현 방법을 모른다. 내 페이지에는 항상 파일 당 하나의 요소/태그 만 포함됩니다. 나는 "hing"이라고 말하면서 "marking_1.html"이라는 말을하고 "h1"이라고 말하면서 하나의 요소 "p"만 갖는 "paragraph.html"을 말합니다. 난 그냥 스크립트를 실행하고 CSS 속성뿐만 아니라 HTML 속성을 얻을 필요가있다. 위의 코드를 어떻게 "구현"합니까? – Jawad

    +0

    요소에서 속성을 가져 오기위한 코드를 추가했습니다. 이 코드를 그대로 구현할 수 있습니다. html 태그 대신 body 태그로 시작할 수있는 것 같습니다. 100 개 항목 임계 값에 도달하는 요소가 충분하지 않으므로 카운터 변수를 제거하지 않아도됩니다. – monitorjbl

    +0

    당신은 나를 짝 지어주지 않습니다. 이 코드로 무엇을해야합니까? JS로 저장하고 스크립트 태그를 사용하여 포함 시키시겠습니까? 위 코드의 JS 파일을 저장하고 "css_prop.js"라고 말하고 "heading_1.html"을 이라고 말합니다. 이것이 그것을 구현하는 방법입니까? – Jawad

    관련 문제