繁体   English   中英

使用jquery从ajax查询导入数据时,要从html中删除/删除第一行吗?

[英]Stripping/removing the first line out of a html when importing data from an ajax query using jquery?

在下面的代码中,我要对其进行编辑,以使其去除第一行。

function update(url,cln){
    $.ajax({
        url: url,
        dataType: "HTML",
        error: function(msg){
            alert(msg.statusText);
            return msg;
        },
        success: function(html){
            $("#main").html(html + '<a id="MAX"></a>');
        }
    });
}

例如,如果代码联系网址: http://stackoverflow.com/ : http://stackoverflow.com/
网址返回:

<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<div id="data">
    <div id="test">
    </div>
</div>

它将删除第一行,以便在将html写入#main时仅写入:

<div id="data">
    <div id="test">
    </div>
</div>

您可以使用正则表达式来匹配从字符串开头到第一个换行符的所有内容:

html = html.replace(/^.*?\r?\n/, '');

演示: http//jsfiddle.net/Guffa/7pdqw/

正则表达式的说明:

^    - matches the beginning of the string
.*?  - matches zero or more character, non-greedy
\r?  - matches zero or one carriage return character
\n   - matches a line feed character

以下将选择您需要的部分内容

function update(url,cln){
    $.ajax({
        url: url,
        dataType: "HTML",
        error: function(msg){
            alert(msg.statusText);
            return msg;
        },
        success: function(html){
            $("#main").html($(html).find('#data').html() + '<a id="MAX"></a>');
        }
    });
}

您应该使用.filter().filter() .load() http://api.jquery.com/load

如何过滤jQuery.ajax返回的数据?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM