gpt4 book ai didi

javascript - 我的代码有什么问题

转载 作者:行者123 更新时间:2023-11-30 23:43:41 25 4
gpt4 key购买 nike

这是我用 JavaScript 编写的第一件事,所以我希望它是一个基本错误

我在这里想要实现的目标:从页面中获取一堆链接,加载其内容查询以查找内部链接,并将它们添加到当前页面上的列表中。

我在 append 时遇到错误在 JQ 文档中,他们说该方法可以获取 JQ 对象

<script type="text/javascript">
$(document).ready(function () {
var wtf = $(".download");
var links = $("a", wtf)
links.each(function (index) {
var newFrame = document.createElement("div");
$(newFrame).load($(this).context.href, function (response, status, xhr) {
if (status == "error") {
alert("error");
}
$("a", response).each(function (index) {
$("#results").append($(this));
});
});
// $("#results").append(newFrame);
});
});
</script>

最佳答案

让我们看看我的猜测错误检查能力是否足够好:

// No point really in using context here.
// This would be better, or at least more readable
var links = $(".download a");

// No need to add in the argument if you're not actually going to use it
links.each(function() {
// Doing this will get you create the jQuery object
// without having to use the DOM createElement method
var newFrame = $('<div />');

// this.href is shorter and neater
newFrame.load(this.href, {
'html': '.ajax'
}, function(response, status, xhr) {
if (status == "error") {
console.log(xhr);
}

// This is the odd part - response is a selector?
// This should loop through each anchor that are a child
// Of the element selected by the response receieved
// Using appendTo here is better than looping through
// each element and using .append() there
$(response).find("a").appendTo("#results");

// And finally of course because this is .load()
// Remember that the actual response is also
// appeneded to the element in question,
// hence the extra .ajax appearing there
});

// This occures outside of callback,
// so it's actually executed *before* the code above
$("#results").append(newFrame);
});

请参阅 jsfiddle 了解这段实际工作的代码:http://jsfiddle.net/E589q/4/

关于javascript - 我的代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3794373/

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