【问题标题】:replacing xml tags using replace function dont works使用替换功能替换 xml 标签不起作用
【发布时间】:2012-07-16 11:09:03
【问题描述】:
<lb/>pis est) les oultragerent grandement les
          <lb/>appellans Trop diteulx, Breschedens,
          <lb/>Plaisans rousseaulx, Galliers, Chien-

我想用
替换标签,以便我可以轻松解析它,我应该使用 javascript 的简单替换功能或一些常规表达式!!!!

//open the document xml
$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "GP.xml",
    dataType: "xml",
    success: parseXml
  });
});

//pasre the document
function parseXml(xml)
{
  //find head and its text in the line breaks 
  $(xml).find("div").each(function()
  {
    $("#output").append($(this).replace(/\n/g, "<br>");
  });
  }

【问题讨论】:

    标签: javascript jquery xml-parsing


    【解决方案1】:

    改变这一行:

    $("#output").append($(this).replace(/\n/g, "<br>"); 
    

    到这里:

    $("#output").append($(this).html().replace(/\n/g, "<br />"); 
    

    使用 $(this) 可以得到对象,但不能得到对象的内容。为此,您需要 .html()。

    编辑:

    $(this) 指的是 $('#output') 而不是 div。

    $(xml).find("div").each(function() {
        var thistext = $(this);
        $("#output").append(thistext.text().replace(/lb/g, "br")); 
    });
    

    EDIT2:

    $(document).ready(function() {
        $.ajax({ 
            type: "GET", 
            url: "lp.xml", 
            dataType: "xml", 
            success: parseXml 
        }); 
    }); 
    
    //parse the document 
    function parseXml(xml) { 
        //find head and its text in the line breaks  
        $(xml).find("div").each(function() { 
            var thistext = $(this); 
            alert(thistext.text());
            $("#output").append(thistext.text().replace(/lb/g, "br"));  
        });
    }
    

    【讨论】:

    • 但问题仍然存在,即使使用 $("#output").append($(this).html().replace(/lb/g, " br"));
    • 这次我用了 replace(/^[lb]+$/, "br" 但没有结果
    • 尝试提醒this.html() 并让您显示是否有任何内容。你的替换功能是正确的replace(/lb/g, "br");
    • 发布更多 GP.xml(或其中的一部分)
    • 试试这一行:$("#output").append(this.text().replace(/lb/g, "br");(text() 而不是 html())。但是当它起作用时,您将拥有&lt;br rend="hyphen"/&gt;
    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多