操纵“数据"以获取数据.来自$ .ajax成功:function(data){ [英] Manipulating the "data" from $.ajax success: function(data) {
问题描述
我有这个和一个简单的问题.
i have this and a simple question to it.
$.ajax({
type: "POST",
url: "/",
data: $(".form").serialize(),
dataType: "html",
success: function (data) {
$("#id").html(data);
}
});
在数据"内部是一些我要插入DOM的html.那没问题.但是我想在操作数据"之前先进行操作.我怎样才能做到这一点?例如,数据"中有一些li元素.例如,在将其插入DOM之前,我将如何从数据"字符串中删除最后一个li元素?
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?
我尝试了
$(data li:last)remove();
...但是那没用.
感谢您的帮助.
推荐答案
您不需要隐藏的DIV.如果要将html字符串转换为DOM片段,只需在其上调用jQuery.在您的示例中:
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:
success: function(data) {
var jqObj = jQuery(data);
jqObj.find("li:last").remove();
$("#id").empty().append(jqObj);
}
一些IE注意事项:
- 如果您的数据很大,则在IE中就地j对其进行量化很慢(至少在jQuery 1.2.6中).在这种情况下,您可能想要为其创建一个容器,例如
var div = jQuery("<div/>"); div.html(data);
,然后在执行$("#id").html(div.html())
之前在该容器中进行操作. - 如果您的数据包含无效的HTML(未关闭的标签等),则您可能会在IE中遇到奇怪的错误.只需确保html的格式正确即可.
- 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.
这篇关于操纵“数据"以获取数据.来自$ .ajax成功:function(data){的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!