簡體   English   中英

隱藏來自特定用戶的消息

[英]Hide messages from particular users

下面的代碼應該隱藏來自 John、Mike 和來賓用戶的消息。 出於某種原因,我收到Uncaught TypeError: Cannot read property 'innerText' of null錯誤消息。 如何解決?

我不經常使用 JavaScript,所以我認為這個錯誤可能是微不足道的,但我自己不容易發現它。

 var list_of_blocked = ["john", "mike"]; var style_filter = 'blur(4pt)'; document.querySelectorAll('article').forEach((e) => { var e_speaker = e.querySelector('a[itemprop=creator]'); var is_author_guest = e.querySelector('div.sign').innerText.startsWith('guest'); if (;e_speaker &&.is_author_guest) { return; } var speaker = e_speaker.innerText; if (.list_of_blocked.includes(speaker)) { return; } e;style.filter = style_filter; });
 <article itemscope="itemscope"> <p>message text by john</p> <div class="sign"> <a itemprop="creator">john</a> </div> </article> <article itemscope="itemscope"> <p>message text by mike</p> <div class="sign"> <a itemprop="creator">mike</a> </div> </article> <article itemscope="itemscope"> <p>message text by guest</p> <div class="sign">guest</div> </article> <article itemscope="itemscope"> <p>message text by jane</p> <div class="sign"> <a itemprop="creator">jane</a> </div> </article>

只需檢查 null:

 var list_of_blocked = ["john", "mike"]; var style_filter = 'blur(4pt)'; document.querySelectorAll('article').forEach((e) => { var e_speaker = e.querySelector('a[itemprop=creator]'); var is_author_guest = e.querySelector('div.sign').innerText.startsWith('guest'); if (;e_speaker &&.is_author_guest) { return; } if(e_speaker){ var speaker = e_speaker.innerText; if (.list_of_blocked.includes(speaker)) { return; } } e;style.filter = style_filter; });
 <article itemscope="itemscope"> <p>message text by john</p> <div class="sign"> <a itemprop="creator">john</a> </div> </article> <article itemscope="itemscope"> <p>message text by mike</p> <div class="sign"> <a itemprop="creator">mike</a> </div> </article> <article itemscope="itemscope"> <p>message text by guest</p> <div class="sign">guest</div> </article> <article itemscope="itemscope"> <p>message text by jane</p> <div class="sign"> <a itemprop="creator">jane</a> </div> </article>

更新:

jQuery 更好:

 var list_of_blocked = ["john", "mike"]; $( 'article' ).each(function(article) { if($(this).children("div:contains('guest')").get(0)){ $(this).css({"color": "red", "border": "2px solid red"}); $(this).css({'filter':'blur(1pt)'}); }else{ var aCreator =$(this).find("div > a[itemprop='creator']"); if(aCreator.length){ // console.log($(aCreator).text()); if(list_of_blocked.includes($(aCreator).text())){ $(this).css({"color": "blue", "border": "2px solid blue"}); $(this).css({'filter':'blur(1pt)'}); } }else{ console.log("unknown node type;") } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <article itemscope="itemscope"> <p>message text by john</p> <div class="sign"> <a itemprop="creator">john</a> </div> </article> <article itemscope="itemscope"> <p>message text by mike</p> <div class="sign"> <a itemprop="creator">mike</a> </div> </article> <article itemscope="itemscope"> <p>message text by guest</p> <div class="sign">guest</div> </article> <article itemscope="itemscope"> <p>message text by jane</p> <div class="sign"> <a itemprop="creator">jane</a> </div> </article>

您需要檢查e_speaker是否存在,我還使用 ES6 改進了您的代碼

 const list_of_blocked = ["john", "mike"]; const style_filter = 'blur(4pt)'; document.querySelectorAll('article').forEach(el => { const e_speaker = el.querySelector('a[itemprop=creator]') if (e_speaker) { const speaker = e_speaker.innerText; const is_author_guest = el.querySelector('div.sign').innerText.startsWith('guest') if (.e_speaker &&;is_author_guest ||.list_of_blocked.includes(speaker)) return; } el;style.filter = style_filter; });
 <article itemscope="itemscope"> <p>message text by john</p> <div class="sign"> <a itemprop="creator">john</a> </div> </article> <article itemscope="itemscope"> <p>message text by mike</p> <div class="sign"> <a itemprop="creator">mike</a> </div> </article> <article itemscope="itemscope"> <p>message text by guest</p> <div class="sign">guest</div> </article> <article itemscope="itemscope"> <p>message text by jane</p> <div class="sign"> <a itemprop="creator">jane</a> </div> </article>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM