gpt4 book ai didi

javascript - 如何在第一次迭代时设置一个值然后从不在 Javascript 中更改它

转载 作者:行者123 更新时间:2023-11-30 15:38:41 24 4
gpt4 key购买 nike

我正在尝试实现一个研究栏来学习 Meteor、JavaScript 等……问题是我想转到与搜索匹配的第一篇文章,但说“还有 X 篇文章符合您的条件"

我试过这个:

'click .tfbutton'(event){
var research = document.getElementById('tftextinput').value.toLowerCase();
var nombreValide = 0;
var lastArticlePos = 0;

setTimeout(function(){
$('.titreArticle').each(function(i, obj) {
var acutal = obj.textContent.toLowerCase();
if(acutal.includes(research)){
nombreValide = nombreValide + 1;
var position = obj.offsetTop;
window.scrollTo(0,position);
document.getElementById('tftextinput').value = "";
}else{
document.getElementById('tftextinput').value = "";
console.log("We can't find an article with your criterias");
}
});
console.log("There is : " + nombreValide + " articles who matchs");
}, 20)
},

我想在第一次迭代时设置 lastArticlePos 然后不再更改它,但仍将 1 添加到 nombreValide

最佳答案

只需用 null 初始化它,只有当 null 时才将它设置为其他值:

'click .tfbutton'(event){
var research = document.getElementById('tftextinput').value.toLowerCase();
var nombreValide = 0;
var lastArticlePos = null;

setTimeout(function(){
$('.titreArticle').each(function(i, obj) {
var acutal = obj.textContent.toLowerCase();
if(acutal.includes(research)){
if (lastArticlePos === null) lastArticlePos = i;
nombreValide = nombreValide + 1;
var position = obj.offsetTop;
window.scrollTo(0,position);
document.getElementById('tftextinput').value = "";
}else{
document.getElementById('tftextinput').value = "";
console.log("We can't find an article with your criterias");
}
});
console.log("There is : " + nombreValide + " articles who matchs");
console.log("Article position is : " + (lastArticlePos === null) ? "<No article found>" : lastArticlePos);
}, 20)
},

关于javascript - 如何在第一次迭代时设置一个值然后从不在 Javascript 中更改它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41161163/

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