我使用AJAX来接收XML响应,然后我需要处理。通过使用jQuery的上下文选项,我可以从XML数据中选择,但我还是不能写入。
$('嗒嗒',XML)
选择XML就好了,但
$('嗒嗒',XML).removeClass('MyClass的')
似乎什么也不做XML变量!我如何能实现我在找的功能?
例如:
VAR数据= NULL;
$(文件)。就绪(函数(){
$阿贾克斯({
键入:GET,
网址:嗒嗒/ blah.jsp
成功:函数(MSG)
{
数据=味精;
在里面();
}
});
功能的init()
{
$('#myElement,数据).removeClass(隐藏); // removeAttr(类)也将失败
}
XML文件示例:
<根>
< DIV>
<! - 大量的内容 - >
< / DIV>
< DIV>
&其中,P的id =myElement&其中; - >中类=隐藏>
测试!
&所述; / P>
< / DIV>
< /根>
解决方案
这对我的作品。
< HTML>
< HEAD>
<冠军>测试页< /标题>
<脚本类型=文/ JavaScript的SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(函数()
{
VAR数据= NULL;
$阿贾克斯({
键入:GET,
网址:sample.xml中,
数据类型:XML,
成功:函数(MSG)
{
初始化($(MSG));
}
});
功能的init($ XML)
{
变量$ myElement = $ xml.find('#myElement');
$ myElement.removeAttr(类);
的console.log($ myElement);
}
});
< / SCRIPT>
< /头>
<身体GT;
< /身体GT;
< / HTML>
和这里的sample.xml中
< XML版本=1.0编码=UTF-8&GT?;
<根>
< DIV>
< / DIV>
< DIV>
&其中,P ID =myElement级=隐藏>
测试!
&所述; / P>
< / DIV>
< /根>
所以一定要确保你的要求与XML
为的dataType
选项,你的JSP页面返回用正确的Content-Type头的内容(文本/ XML)
I'm using AJAX to receive an XML response, which I then need to manipulate. By using jQuery's context option, I can select from the XML data, but I am still unable to write to it.
$('blah', xml)
selects xml just fine, but
$('blah', xml).removeClass( 'myClass' )
seems to do nothing to the xml variable! How can I achieve the functionality I'm looking for?
Example:
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
}
Example xml file:
<root>
<div>
<!--lots of content -->
</div>
<div>
<p id = "myElement<->" class = "hidden">
Test!
</p>
</div>
</root>
解决方案
This works for me.
<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>
And here's sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<div>
</div>
<div>
<p id = "myElement" class = "hidden">
Test!
</p>
</div>
</root>
so make sure you are requesting with "xml"
as the dataType
option, and that your JSP page returns the content with the correct Content-Type header (text/xml)