简体   繁体   English

为什么我的if语句与else if而不是OR运算符一起工作

[英]Why does my if statement work with an else if, but not an OR operator

I wrote a small piece of JS in the console, to loop through recommended connections on LinkedIn and if the text contains a certain word, ignore that card, otherwise click the 'X' close button. 我在控制台中写了一小段JS,以遍历LinkedIn上推荐的连接,如果文本中包含特定单词,请忽略该卡片,否则单击“ X”关闭按钮。

Initially I wrote it like this: 最初,我是这样写的:

const list = document.querySelector('.mn-pymk-list__cards');
const cards = list.querySelectorAll('.mn-pymk-list__card');

cards.forEach( (card, i) => {

  setTimeout( ()=>{
    let text = card.querySelector('.member-insights__count');

    if( !text.textContent.includes('Sharon') || text === null ) {
        card.querySelector('.pymk-card__close-btn').click();
       } else {
        card.style.background = 'green';
       }
  }, i * 1000 )

});

However, when it ran, it would sometimes error (whilst continuing to iterate) with 'Could not read textContent of null'. 但是,当它运行时,有时会出现“无法读取null的textContent”错误(直到继续进行迭代)。

However, when I wrote the code like this: 但是,当我编写如下代码时:

const list = document.querySelector('.mn-pymk-list__cards');
const cards = list.querySelectorAll('.mn-pymk-list__card');

cards.forEach( (card, i) => {

  setTimeout( ()=>{
    let text = card.querySelector('.member-insights__count');

    if( text === null ) {
        card.querySelector('.pymk-card__close-btn').click();
    } else if (!text.textContent.includes('Sharon')) {
        card.querySelector('.pymk-card__close-btn').click();
    } else {
        card.style.background = 'green';
    }
  }, i * 1000 )

});

It runs absolutely fine and does what I want it to. 它运行得很好,并且可以满足我的要求。

QUESTION: I can't understand why the first option doesn't work, as it seems more concise and theoretically should do the same thing? 问题:我不明白为什么第一个选项不起作用,因为它看起来更加简洁,理论上应该做同样的事情?

I suspect it has something to do with the fact that on LinkedIn, some of the suggested contacts don't have a class of '.member-insights__count' and instead have '.member-insights__info'. 我怀疑这与以下事实有关:在LinkedIn上,某些建议的联系人没有“ .member-insights__count”类,而是具有“ .member-insights__info”。

But, that should still make text resolve to null, right? 但是,那仍然应该使文本解析为null,对吗?

Any insight would be great! 任何见识都会很棒!

if( !text.textContent.includes('Sharon') || text === null ) {

What would you expect this code to do when text is null? text为null时,您希望此代码做什么? It will crash because you can't access properties or methods of null. 它将崩溃,因为您无法访问null的属性或方法。

You need to check for null first. 您需要先检查null。

if( text === null || !text.textContent.includes('Sharon') ) {

If textContent is null (eg as you said, the contact has '.member-insights__info' instead), !text.textContent.includes('Sharon') will cause the error message. 如果textContent为null(例如,如您所说,联系人使用的是“ .member-insights__info”),则!text.textContent.includes('Sharon')将导致错误消息。

In the second version, that line can only be reached if the text isn't null. 在第二个版本中,仅当文本不为null时才能到达该行。

The || || operator will only evaluate the 2nd argument if the first argument is false. 如果第一个参数为false,则运算符将仅计算第二个参数。

In your case the only difference between the first and second code snippets is the order in which you check if the value is null 在您的情况下,第一段和第二段代码之间的唯一区别是检查值是否为null的顺序

What you need to do is check for null first like this: 您需要做的是先检查null,如下所示:

if( text === null ||  !text.textContent.includes('Sharon')) {

This way it will only check if text includes specific text once it's determined that text is not null 这样,只有在确定text不为null时,它才会检查text包含特定文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM