Jquery.html()无效

时间:2014-02-28 11:15:13

标签: javascript jquery html xml

我试图提醒$ xml.html()的值,但我得到了未定义。有我的代码

var xml ='<xml version="1.0" encoding="UTF-8" standalone="no"><TaxPayer><PersonalInformation><FirstName>aashu</FirstName></PersonalInformation></TaxPayer></xml>';
var xmlDoc = $.parseXML( xml );
var $xml = $( xmlDoc );
alert(xml);
$xml.find('TaxPayer').text('shri');
alert($xml.html());

2 个答案:

答案 0 :(得分:2)

<强> JS FIDDLE DEMO

<强> JS FIDDLE DEMO 2

var xml ='<xml version="1.0" encoding="UTF-8" standalone="no"><TaxPayer><PersonalInformation><FirstName>aashu</FirstName></PersonalInformation></TaxPayer></xml>';
var xmlDoc = $.parseXML( xml );
var $xml = $( xmlDoc );
alert(xml);
alert($xml.find('xml').html());
alert($xml.find('xml').text());

var xml ='<xml version="1.0" encoding="UTF-8" standalone="no"><TaxPayer><PersonalInformation><FirstName>aashu</FirstName></PersonalInformation></TaxPayer></xml>';
var xmlDoc = $.parseXML( xml );
var $xml = $( xmlDoc );
alert(xml);
alert($xml.find('xml').html());
alert($xml.find('xml').text());
$xml.find('FirstName').text('satinder');

alert($xml.find('xml').html());
alert($xml.find('xml').text());

<强> Learn jQuery: Simple and easy jQuery tutorials blog

答案 1 :(得分:0)

试试这个

    var xml =$('<XMLDocument />')
    xml.append($('<TaxPayer/>')
               .append($('<PersonalInformation/>')
                       .append($('<FirstName/>').text('aashu')))); 
var xmlDoc = $.parseXML( xml );
var $xml = $( xmlDoc );
alert(xml.html());
xml.find('TaxPayer').find('FirstName').text('shri');
alert(xml.html());

这可能会有所帮助。 live demo