0

多亏了 jQuery,我得到了一个 XHTML 页面(存储在我的计算机上)的 url,我想解析这个页面,然后删除我的 XML 项目。

我的 XHTML 页面

<html>
 ...
    <exampleXML>
        <dataset>a</dataset>
        <dataset>b</dataset>
        <dataset>c</dataset>
    </exampleXML>
...
</html>

我的 jQuery 代码

var url_val = $("#xhtml_page").val();
$.get(url_val, function(data) {
    $(data).find("dataset").each(function() {
        if ($(this).text() == "a"){
           $(this).remove();
        }
    });
    $("#body-tmpl").html('<div id="id_container"></div>');
    $("#id_container").html($(data));
});

当我的项目被删除时,我会将我的新 XHTML 的内容发送到一个 div 中。

但不幸的是,我无法删除 XHTML 元素(示例 a)并将新页面放在 div 中。这是最好的解决方案吗?

在尝试删除项目之前,我将 XHTML 放在 iframe 中。

<iframe frameborder="0" style="width:100%; height:100%;" src="XHTMLfile.html">
#document<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org   /TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head> … </head>
    <body style="overflow: hidden; margin: 0px; font: 11px sans-serif; cursor: auto;">
    <img id="hiddenImage" style="display:none" src="http://soft.sourceforge.net        /img/hidden.png"></img>
    <img id="loadingImage" style="display:none" src="http://soft.sourceforge.net             /img/loading.gif"></img>
    <img id="logo" style="display:none" src="http://soft.sourceforge.net/img/logo.png"> </img>
    <noscript> … </noscript>
    <div style="display:none"> … </div><div style="position: absolute; top: 1%; right: 2%;      text-align: right;"> … </div>
    <input type="button" value="x" style="position: fixed; visibility: visible; right: 10.5px; top: 492.333px;"></input>
    <canvas width="1000" height="700"></canvas>
    <div style="position: absolute; top: 0px;"> … </div>
</body>
</html>
</iframe>

现在当我把它放在一个 div 中时我有了它:

    <div id="id_container" style="width:1000px; height:700px;">
    <meta charset="utf-8">
    </meta><link href="http://soft.sourceforge.net/img/favicon.ico" rel="shortcut icon">
    </link><img id="hiddenImage" style="display:none" src="http://soft.sourceforge.net/img/hidden.png"></img>
    <img id="loadingImage" style="display:none" src="http://soft.sourceforge.net/img/loading.gif"></img>
    <img id="logo" style="display:none" src="http://soft.sourceforge.net/img/logo.png"></img>
    <noscript> … </noscript>
   <div style="display:none">
    … </div>
   </div>
4

1 回答 1

0

关键是你不要改变data. data您总是使用 . 一次又一次地解析字符串(响应)$(data)。您应该将$(data)一次分配给一个变量并对其进行操作。像这样

$.get(url_val, function(data) {
    var $data = $(data);
    $data.find("dataset").each(function() {
        if ($(this).text() == "a") {
            $(this).remove();
        }
    });
    $("#body-tmpl").html('<div id="id_container"></div>');
    $("#id_container").html($data);

});

于 2013-07-26T07:42:32.507 回答