2015-01-23 8 views
1

HTML이 포함 된 간단한 콘텐츠에서 jQuery의 XML 파서를 사용하고 있습니다.IE9에서 jQuery로 XML을 구문 분석 할 때 Chrome이 아닌 innerHTML이 누락되었습니다.

크롬에서 잘 작동 .innerHTML jQuery의 .html() 또는 표준 자바 스크립트를 사용하여 전체 HTML 텍스트를 추출

,하지만 두 경우 모두에서 인터넷 익스플로러 9 jQuery의 .text() 작품,하지만 나는 HTML 태그뿐만 아니라 추출해야합니다.

IE9에서도 어떻게 작동합니까?

당신은 여기를 테스트 할 수 http://jsfiddle.net/199vLsgz/

XML :

<script id="xml_data" type="text/xml"> 
    <model_data name="The model "> 
      <person_responsible></person_responsible> 
      <objects> 
       <object name="Available B reports" id="obj1" > 
        <description>this is a description <br/> oh look, another line!</description> 
       </object> 
      </objects> 
    </model_data> 
</script> 

코드 :

XML 요소는 innerHTML을 함께 사용하고 있는지되는 IE 내부에서 정의하지 않습니다
$(function() { 

    var xml = $("#xml_data").text(); 
    var xmlDoc = $.parseXML(xml); 
    $xml = $(xmlDoc); 

    var desc = $xml.find("model_data > objects > object[id='obj1'] > description"); 
    alert(desc.html()); 

}) 

답변

2

jquery의 html 함수.

Firstly you need to use CDATA to preserve tags inside xml tags

<description><![CDATA[this is a description <br/> oh look, another line!]]></description>

다음은는 TextContent 속성을 사용하려고 할 수 있습니다

alert(desc[0].textContent); //desc.text() will also work now 

을 그리고 당신은 또한 정확하게 같은 것을 사용하여 콘텐츠를 추가 할 수 있습니다

$('#some-container').html(desc[0].textContent); 

$(function() { 
 

 
    var xml = $("#xml_data").text(); 
 
    var xmlDoc = $.parseXML(xml); 
 
    $xml = $(xmlDoc); 
 
    console.log($xml) 
 
    var desc = $xml.find("model_data > objects > object[id='obj1'] > description"); 
 
    alert(desc[0].textContent); 
 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script id="xml_data" type="text/xml"> 
 
    <model_data name="The model"> 
 
      <person_responsible></person_responsible> 
 
      <objects> 
 
       <object name="Available B reports" id="obj1" > 
 
        <description><![CDATA[this is a description <br/> oh look, another line!]]></description> 
 
       </object> 
 
      </objects> 
 
    </model_data> 
 
</script>