我的確注意了一些其他的答案,所以在這方面,和from one of them我發現我在這裏使用的一個,但它看起來像它永遠不會發生,因爲這不是return false;
發射console.log(myId);
通常應該。如何查找.each()的結尾?
let pattern = new RegExp('^\/wiki\/');
var total = doSelect("Location").siblings('td').find('a').length;
doSelect("Location").siblings('td').find('a').each(function(i, el, index) {
var result = $(this).attr('href').replace(pattern, '');
console.log(result);
if (index === total - 1) {
myId = item.pageid;
console.log(myId);
return false;
}
});
我甚至嘗試與沒有積極成果this other SO answer建議如下:
let pattern = new RegExp('^\/wiki\/');
doSelect("Location").siblings('td').find('a').each(function(i, el) {
var result = $(this).attr('href').replace(pattern, '');
console.log(result);
}).promise().done(function(){
myId = item.pageid;
console.log(myId);
return false;
});
UPDATE
關於:item
(用下面的數據沒有問題),上述代碼在以下範圍內data each
:
$.getJSON(url,function(data){
$.each(data, function(i, item) {
是什麼'item'? –
@ er-han更新了您的問題的答案,以防其他人需要 –
您不應該在if語句中簡單地使用'i'而不是'index'嗎? –