je dois analyser les valeurs de cette source HTML: Comment puis-je faire cela avec jQuery / JS en boucle?

"attributelist - key" est le nom de colonne DB cible.

<dl class="attributelist-striped">
    <dt class="attributelist--key">Lorem Ipsum:</dt>
    <dd class="attributelist--value">value</dd>
    <dt class="attributelist--key">Lorem Ipsum1:</dt>
    <dd class="attributelist--value">value1</dd>
    <dt class="attributelist--key">Lorem Ipsum2:</dt>
    <dd class="attributelist--value">value2</dd>
    <dt class="attributelist--key">Lorem Ipsum3:</dt>
    <dd class="attributelist--value">value3</dd>
    <dt class="attributelist--key">Lorem Ipsum4:</dt>
    <dd class="attributelist--value">value4</dd>
    <dt class="attributelist--key">Lorem Ipsum5:</dt>
    <dd class="attributelist--value">value5</dd>
</dl>
-3
beeseven 8 nov. 2019 à 15:55

1 réponse

Vous pouvez analyser le code HTML à l'aide de la fonction jquery $ .parseHTML ()

var html='<dl class="attributelist-striped"><dt class="attributelist--key">Lorem Ipsum:</dt><dd class="attributelist--value">value</dd><dt class="attributelist--key">Lorem Ipsum1:</dt> <dd class="attributelist--value">value1</dd> <dt class="attributelist--key">Lorem Ipsum2:</dt>   <dd class="attributelist--value">value2</dd>  <dt class="attributelist--key">Lorem Ipsum3:</dt> <dd class="attributelist--value">value3</dd> <dt class="attributelist--key">Lorem Ipsum4:</dt> <dd class="attributelist--value">value4</dd><dt class="attributelist--key">Lorem Ipsum5:</dt><dd class="attributelist--value">value5</dd></dl>'

var p=$.parseHTML(html)
var z=p[0].children
for(var i=0;i<z.length;i++){ console.log(z[i].innerHTML)}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
0
Negi Rox 8 nov. 2019 à 13:08