2
votes

JQuery ': contient' le convertir en Javascript

Mon problème est que le code est écrit en Jquery, je veux le convertir en Javascript mais je ne trouve pas la documentation de javascript sur contient.

Si vous voulez savoir comment cela fonctionne, alors laissez-moi vous expliquer. À l'intérieur d'une table, il y a les mots «CRITIQUE» et «SÛR», cela comptera le nombre de CRITIQUES qu'il y a à l'intérieur de la table. Donc, le résultat attendu ici est:

var test = $('td:contains(CRTICAL)');

console.log(test.size());

Btw ajouter un nom de classe ou un ID n'est pas une option, donc dans la mesure du possible, veuillez ne pas utiliser le nom de la classe ou le nom de l'ID.

file.php
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<table>
<tr>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>SAFE</td>
</tr>
<tr>
 <td>SAFE</td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
</tr>
</table>

FICHIER JS

6

p>


0 commentaires

3 Réponses :


2
votes

Vous pouvez créer une fonction réutilisable qui accepte le tableau d'éléments et le texte à rechercher, puis renvoyer le nombre de cette fonction:

<table>
  <tr>
    <td>CRITICAL </td>
    <td>CRITICAL </td>
    <td>CRITICAL </td>
    <td>SAFE</td>
  </tr>
  <tr>
    <td>SAFE</td>
    <td>CRITICAL </td>
    <td>CRITICAL </td>
    <td>CRITICAL </td>
  </tr>
</table>
var test = document.querySelectorAll('table tr td');

function getCount(elem, text) {
  var count = 0;
  for (var i = 0; i < elem.length; i++) {
    if (elem[i].innerText.trim().indexOf(text) !== -1) {
      count++;
    }
  }
  return count;
}


console.log('CRITICAL-->' + getCount(test, 'CRITICAL'));
console.log('SAFE-->' + getCount(test, 'SAFE'));


0 commentaires

2
votes

Peut être fait en une seule ligne de code

<table>
<tr>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>SAFE</td>
</tr>
<tr>
 <td>SAFE</td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
</tr>
</table>

Explication:

  1. [] .slice.call (document.querySelectorAll ("td")) convertit le retour de liste de nœuds par sélecteur javascript en un tableau afin que vous puissiez utiliser la fonction filter
  2. .filter (el => el.innerText.indexOf ("CRITICAL")! == -1 filtre tout élément td qui ne contient pas "CRITICAL"

Extrait:

var test = [].slice.call(document.querySelectorAll("td")).filter(el => el.innerText.indexOf("CRITICAL") !== -1);

console.log(test.length);
[].slice.call(document.querySelectorAll("td")).filter(el => el.innerText.indexOf("CRITICAL") !== -1)


0 commentaires

2
votes

Vous pouvez lui créer une fonction personnalisée.

<table>
<tr>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>SAFE</td>
</tr>
<tr>
 <td>SAFE</td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
 <td>CRITICAL </td>
</tr>
</table>
let word = 'CRITICAL';
function contains(elements,string){
  let count= 0;
  elms = Array.from(elements);
  elms.forEach(elm => {
    if(elm.innerText.includes(string)) count++;
  })  
  return count;
}
console.log(contains(document.querySelectorAll('table td'),word));


0 commentaires