2017-08-13 19 views
1

Je souhaite analyser du contenu html à partir d'une zone de texte et obtenir une valeur à partir d'une balise sélectionnée à l'aide de jquery. J'ai essayé le code ci-dessous mais aucun espoir.Analyse HTML à partir d'une zone de texte à l'aide de jquery

var a = $('#a').val(); 
 
var dom_nodes = $($.parseHTML(a)); 
 
alert(dom_nodes.find('#ae').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<textarea id="a"> 
 
<?xml version="1.0" encoding="UTF-8" ?> 
 
<!DOCTYPE html> 
 
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> 
 
<b:if cond='data:skin.vars.body_background.image and data:features.responsiveBackgrounds' id='ae'> 
 
     <b:with value='&quot;body&quot;' var='selector'> 
 
     <b:include cond='not data:view.isPreview' data='skin.vars.body_background.image' name='responsiveImageStyle'/> 
 
     </b:with> 
 
    </b:if> 
 
    </html> 
 
</textarea>

+0

L'erreur indique clairement que vous n'avez pas inclus la bibliothèque jQuery. – PeterKA

+0

Il vous manque la référence de bibliothèque jQuery à la page. – iMatoria

+0

même après avoir inclus jquery je ne le html interne –

Répondre

0

Vous pouvez utiliser DomParser pour obtenir le nœud ae.

var xml = document.getElementById('a').value; 
 
var parser = new DOMParser(); 
 
var xmlDoc = parser.parseFromString(xml, "text/xml"); 
 
var ae = xmlDoc.getElementById('ae'); 
 
console.log(ae.id);
<textarea id="a"> 
 
<?xml version="1.0" encoding="UTF-8" ?> 
 
<!DOCTYPE html> 
 
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> 
 
<b:if cond='data:skin.vars.body_background.image and data:features.responsiveBackgrounds' id='ae'> 
 
     <b:with value='&quot;body&quot;' var='selector'> 
 
     <b:include cond='not data:view.isPreview' data='skin.vars.body_background.image' name='responsiveImageStyle'/> 
 
     </b:with> 
 
    </b:if> 
 
    </html> 
 
</textarea>

1

Utilisez parseXML à la place.

var a = $('#a').val(); 
 
var dom_nodes = $($.parseXML(a)); 
 
alert(dom_nodes.find('#ae').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<textarea id="a"> 
 
<?xml version="1.0" encoding="UTF-8" ?> 
 
<!DOCTYPE html> 
 
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> 
 
<b:if cond='data:skin.vars.body_background.image and data:features.responsiveBackgrounds' id='ae'> 
 
     <b:with value='&quot;body&quot;' var='selector'> 
 
     <b:include cond='not data:view.isPreview' data='skin.vars.body_background.image' name='responsiveImageStyle'/> 
 
     </b:with> 
 
    </b:if> 
 
    </html> 
 
</textarea>

+0

pour cet exemple de code .. la solution ci-dessus fonctionne bien ... mais je fais face à beaucoup de code qui se trouve dans la zone de texte. Dans ce cas, il ne montre aucun :( –

+0

Pourriez-vous vérifier la console pour les erreurs – Poootaatoooo

+0

et la plupart des tags qui vient dans la zone de texte n'a pas de balise de fin comme xml fait .. –