我只是想知道是否会消失,我可以检查具有相同类名的最后一个div是否包含某个单词,并且该单词是否存在运行一个函数。
每次我尝试寻求帮助时,我找到的全部都在jquery中
var words = #hey, #join, #slap
var nodes = document.querySelectorAll('.hud-chat-message');
var last = nodes[nodes.length- 1];
// Check for certain words here in nodes last
<div id="hud-chat" class="hud-chat">
<div class="hud-chat-message">
<strong>Name</strong>: text here
</div>
<div class="hud-chat-message">
<strong>Name</strong>: text here
</div>
<div class="hud-chat-message">
<strong>Name</strong>: just more text here
</div>
<div class="hud-chat-message">
<strong>Name</strong>: text here
</div>
<div class="hud-chat-message">
<strong>Name</strong>: /join
</div>
<div class="hud-chat-message">
<strong>Name</strong>: text here
</div>
<div class="hud-chat-message">
<strong>Name</strong>: /slap
</div>
</div>
答案 0 :(得分:1)
您可以这样做。单词应该是包含所有单词的数组。然后,您只需使用indexOf
遍历单词并检查它们是否为字符串的一部分即可。
var words = ['hey', 'join', 'slap'];
var nodes = document.querySelectorAll('.hud-chat-message');
var last = nodes[nodes.length- 1];
for(var i = 0; i < words.length; i++) {
if(last.innerText.indexOf(words[i]) > -1) {
console.log('Found: ' + words[i]);
}
}
<div id="hud-chat" class="hud-chat">
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: just more text here</div>
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: /join</div>
<div class="hud-chat-message"><strong>Name</strong>: text here</div>
<div class="hud-chat-message"><strong>Name</strong>: /slap</div>
</div>