jQuery 从 $.ajax 操作“数据”成功:function(data) {

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1878118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:19:26  来源:igfitidea点击:

Manipulating the "data" from $.ajax success: function(data) {

jquery

提问by Hans

i have this and a simple question to it.

我有这个和一个简单的问题。

$.ajax({
    type: "POST",
    url: "/",
    data: $(".form").serialize(),
    dataType: "html",
    success: function (data) {
        $("#id").html(data);
    }
});

Inside "data" is some html I am inserting into the DOM. Thats no problem. But I want to manipulate the "data" before doing so. How can I do that? For example there are some li elements in "data". How would I for example delete the last li element out of the "data" string before inserting it into the DOM?

在“数据”里面是我插入到 DOM 中的一些 html。那没问题。但我想在这样做之前操纵“数据”。我怎样才能做到这一点?例如,“data”中有一些 li 元素。例如,在将“数据”字符串插入到 DOM 之前,我将如何从“数据”字符串中删除最后一个 li 元素?

I tried something like

我试过类似的东西

$(data li:last)remove();

...but that didnt work.

...但这没有用。

Thanks for your help.

谢谢你的帮助。

回答by jpsimons

You don't need a hidden DIV. If you want to convert an html string to a DOM fragment, simply call jQuery on it. In your example:

您不需要隐藏的 DIV。如果要将 html 字符串转换为 DOM 片段,只需在其上调用 jQuery。在你的例子中:

success: function(data) {
    var jqObj = jQuery(data);
    jqObj.find("li:last").remove();
    $("#id").empty().append(jqObj);
}

Some IE considerations though:

一些 IE 考虑因素:

  • If your data is large, jQuerifying it in-place is slow in IE (at least with jQuery 1.2.6). In that case you may want to create a container for it like var div = jQuery("<div/>"); div.html(data);then manipulate it there before doing $("#id").html(div.html()).
  • If your data has invalid HTML (unclosed tags, etc) you may get weird failures in IE. Just make sure the html is well-formed.
  • 如果您的数据很大,在 IE 中就地进行 jQuerifying 会很慢(至少在 jQuery 1.2.6 中)。在这种情况下,您可能希望为它创建一个容器,var div = jQuery("<div/>"); div.html(data);然后在执行$("#id").html(div.html()).
  • 如果您的数据包含无效的 HTML(未关闭的标签等),您可能会在 IE 中遇到奇怪的故障。只要确保 html 格式正确。

回答by Dave Ward

You can create a jQuery object from arbitrary HTML, e.g.:

您可以从任意 HTML 创建一个 jQuery 对象,例如:

$('<ul><li>Item</li></ul>');

So, you could do something like this:

所以,你可以做这样的事情:

success: function(data) {
  var $list = $(data);

  $list.find('li:last').remove();

  // Do something with $list here, like append().
}

Here's a working example you can play around with on JS Bin: http://jsbin.com/ejule3/edit

这是您可以在 JS Bin 上使用的工作示例:http: //jsbin.com/ejule3/edit

回答by Jay

The data returned is a string, and cannot be found in the dom via jQuery until it's actually in the DOM. You will need to parse this string and pull out the data you require, or edit it after you insert in the DOM.

返回的数据是一个字符串,在它实际存在于 DOM 之前无法通过 jQuery 在 dom 中找到。您将需要解析此字符串并提取您需要的数据,或在插入 DOM 后对其进行编辑。

回答by leepowers

Jay's answeris right - you will need to insert the HTML data into the DOM first (or parse the string, which seems cumbersome). What you can do is append the data to a hidden DIV, modify it, then copy the data to a different DOM element or just show the DIV

Jay 的回答是对的——你需要先将 HTML 数据插入到 DOM 中(或者解析字符串,这看起来很麻烦)。您可以做的是将数据附加到 hidden DIV,修改它,然后将数据复制到不同的 DOM 元素或只显示DIV