0

I have a fiddle: http://jsfiddle.net/j8Y76/

When you click Process log, it should display the contents of the "to" tag. (on the console).

$('#doProcessLog').click(function(){
    var XMLstring = $.parseXML($.trim($('#log-a').val()));
    var out = $(XMLstring).find('note to').each(function(i, data){
        console.log(data);
    });
}); 

However, It displays the whole element with the tags, but if I try to output it to an alert box or div element as text, it comes back as "Object "

4

3 回答 3

0

那是因为您的代码段中的数据确实是一个对象。如果您需要将其附加到某个 div,请尝试 $("#div").append(data) 或者,如果您需要在警报框中显示标签,请尝试 alert(data.outerHTML)

于 2013-09-05T13:58:39.380 回答
0

尝试

console.log($(data).text());

演示:小提琴

如果您只期望一个to节点,那么

$(document).ready(function () {
    $('#doProcessLog').click(function () {
        var XMLstring = $.parseXML($.trim($('#log-a').val()));
        var out = $(XMLstring).find('note to').text();
        console.log('out:', out)
    });
});

演示:小提琴

于 2013-09-05T13:49:16.083 回答
0

你只需要.textContent像这样添加

$(document).ready(function(){
    $('#doProcessLog').click(function(){
        var XMLstring = $.parseXML($.trim($('#log-a').val()));
        var out = $(XMLstring).find('note to').each(function(i, data){
            console.log(data.textContent);
        });
    }); 
});

希望这有帮助!

于 2013-09-05T13:52:40.350 回答