2009-09-28 2 views
0

저는 AJAX를 사용하여 XML 응답을 수신하고 있습니다.이 응답은 이후 조작해야합니다. jQuery의 컨텍스트 옵션을 사용하면 XML 데이터에서 선택할 수 있지만 여전히 쓸 수는 없습니다.jQuery로 XML을 포함하는 변수 조작?

$('blah', xml) 

이 잘 XML을 선택하지만

$('blah', xml).removeClass('myClass') 

는 XML 변수에 아무것도하지 않는 것 같다! 원하는 기능을 어떻게 얻을 수 있습니까?

예 :

var data = null; 

$(document).ready(function(){ 
$.ajax({ 
    type:"GET", 
    url:"blah/blah.jsp", 
    success:function(msg) 
    { 
     data = msg; 
     init(); 
    } 
}); 

function init() 
{ 
    $('#myElement', data).removeClass('hidden');//removeAttr('class') also fails 
} 

예 XML 파일 :

<root> 
<div> 
<!--lots of content --> 
</div> 
<div> 
<p id = "myElement<->" class = "hidden"> 
    Test! 
</p> 
</div> 
</root> 
+0

XML 파일이 표시 될 수 있습니까? –

+0

addClass/removeClass는 HTML에 따라 다르며 class라는 속성을 수정합니다. removeAttr이 작동하는지 알려줄 수 있습니까? –

+0

removeAttr이 작동하지 않습니다. –

답변

1

이 나를 위해 작동합니다.

<html> 
<head> 
    <title>Test Page</title> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
    <script type="text/javascript"> 
    $(function() 
    { 
    var data = null; 
    $.ajax({ 
     type:"GET", 
     url:"sample.xml", 
     dataType: 'xml', 
     success:function(msg) 
     { 
      init($(msg)); 
     } 
    }); 

    function init($xml) 
    { 
     var $myElement = $xml.find('#myElement'); 
     $myElement.removeAttr('class'); 
     console.log($myElement); 
    } 
    }); 
    </script> 
</head> 

<body> 

</body> 
</html> 

그리고 여기가 sample.xml의

그래서
<?xml version="1.0" encoding="UTF-8"?> 
<root> 
<div> 

</div> 
<div> 
<p id = "myElement" class = "hidden"> 
    Test! 
</p> 
</div> 
</root> 

당신이 dataType 옵션으로 "xml"으로 요청하는, 그리고 JSP 페이지가 올바른 컨텐츠 유형 헤더와 내용을 반환해야합니다 (텍스트/xml)

+0

* 반복적으로 투표를 클릭하여 100으로 맞춰질 수 있기를 바랍니다. * jsp 응답 헤더의 내용 유형. 이 미묘한 버그. –

+0

또한 msg 대신 msg.d를 사용할 필요가 있음을 알고 있어야합니다. http://encosia.com/2009/02/10/a-breaking-change-between-versions-of-aspnet-ajax/ – Maslow

관련 문제