gpt4 book ai didi

jquery - 嵌套jquery的.append()、.each()、append()并保留第一个 "this"引用

转载 作者:行者123 更新时间:2023-12-01 07:31:14 28 4
gpt4 key购买 nike

请考虑以下代码片段:

            var ul = $(".list_b").find("li").remove().end();
$.each(Sites, function (index, value) {
ul.append(
$("<li></li>")
.append(
$("<span></span>")
.addClass("img_b")
.css("background-image", "url(pin.png)")
.text(value.Name)
)
.append(
$("<div></div>")
.addClass("div_archives")
.each(value.Documents, function (index2, value2) {
this.append(
$("<a></a>")
.attr({ href: value.URL })
.attr({ target: "_blank" })
.html(value2.Name)
.append(
$("<img />")
.attr({ src: "../img/icon_" + value2.Type + ".png" })
.attr({ alt: "Archive " + value2.Tipo })
)
)
}
)
)
);
});

并将这个 json 作为源对象。

[   {
"Id":1,
"Name":"Rachel Weiss",
"Color":"FF0000",
"Symbol":"A",
"Latitude":51.123123,
"Longitude":12.131231,
"Documents":[
{
"Id":1,
"Name":"Document 1",
"Ext":"pdf",
"Type":"pdf",
"URL":"http://www.google.com"
},
{
"Id":2,
"Name":"Document 2",
"Ext":"doc",
"Type":"word",
"URL":"http://www.yahoo.com"
},
{
"Id":3,
"Name":"Document 3",
"Ext":"xls",
"Type":"excel",
"URL":"http://www.amazon.com"
}
],
"Notes":[
{
"Id":0,
"Texto":"test test"
},
{
"Id":1,
"Texto":"test test. test test"
}
]},{
"Id":2,
"Name":"Natalie Portman",
"Color":"00FF00",
"Symbol":"B",
"Latitude":51.123123,
"Longitude":12.131231,
"Documents":[
{
"Id":11,
"Name":"Document 11",
"Ext":"doc",
"Type":"word",
"URL":"http://www.google.com"
}
],
"Notes":[
{
"Id":10,
"Texto":"test test. test test. test test."
}
]} ... ]

我试图实现的目标是附加 <a></a>阻止父级 <div></div> block ,但我找不到引用外部“this”对象的方法。

我知道这一定是一个蹩脚的问题,但我没有任何线索,所以每个想法都会受到欢迎。

提前致谢。

<小时/>

附加信息:两者都不是this也不$(this)有效,两种方式都会引发运行时异常(对象不支持此属性或方法)

最佳答案

在您需要的内部,将其分配给一个变量,因此它将在...之后可用

说你有

$("myelement").click(function() {
var elementtoneed = this;
$("myotherelement").load('sample.html', function() {
$(elementtoneed).hide();
});
});

这样,您就可以保存“this”元素以供将来引用。

关于jquery - 嵌套jquery的.append()、.each()、append()并保留第一个 "this"引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5944609/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com