2013-07-04 16 views
5

Ho HTML con diversi tag personalizzati. Voglio trovare tutti tranne due ('start', 'end') e scartarli. jQuery.find() sembra trovare solo questi tag personalizzati quando cerco cosa c'è nel documento, non quando cerco un oggetto jQuery. Che cosa sto facendo di sbagliato?jQuery non trova tag personalizzati

dovrebbe essere auto-esplicativo del violino:

http://jsfiddle.net/hpNN3/2/

Ecco la parte javascript:

var raw = $('pre').html(); 
var html = $(raw); 
var starts = html.find('start'); 
var spans = html.find('span'); 

//this returns nothing 
console.log(starts) 
// works - can find in object 
console.log(spans) 
//this works 
console.log($('start')); 


//only picks up spans, not annotations 
// I want this to return the innerHTML of the pre, stripping all tags except for 'start' and 'end' -- but retain the contents of those tags. 
var cleaned = html.find(':not(start, end)').each(function() { 
    $(this).contents().unwrap(); 
}); 

console.log(cleaned); 

$('#clean').html(cleaned) 

e un esempio di HTML:

<span class="ng-scope">CTAGCTCTCTGGAGATTAACGAGGAGAAATACTAGAtTGGTTCAT</span> 
<start feat="1" class="ng-scope"></start> 
<annotation index="1" class="ng-isolate-scope ng-scope" style="background-color: rgb(238, 153, 238); background-position: initial initial; background-repeat: initial initial;"> 
    <span tooltip="Another Promoter" tooltip-placement="mouse" tooltip-append-to-body="true" ng-transclude="" class="ng-scope"> 
     <span class="ng-scope">GATCATAAgcttgaat</span> 
    </span> 
</annotation> 
<end feat="1" class="ng-scope"></end> 
<span class="ng-scope">tagccaaacttatt</span> 

che dovrebbe be:

CTAGCTCTCTGGAGATTAACGAGGAGAAATACTAGAtTGGTTCAT<start feat="1" class="ng-scope"></start>GATCATAAgcttgaat<end feat="1" class="ng-scope"></end>tagccaaacttatt

Grazie

+4

html con tag personalizzati non è più html –

+1

è con l'eccezione di

+2

Perché scrivere HTML in questo modo? Quali vantaggi ha il tag '' hanno più di '

' o ''? Per non parlare, è più veloce usare il selettore '$ ('. ClassName')'. – Dom

risposta

3

Il vostro problema si trova con la variabili iniziali:

var raw = $('pre').html(); 
var html = $(raw); 

Questo si traduce in var html = $($('pre').html()), che non corrisponderà alcun elemento. La ragione è che, dal momento che il selettore non è preceduta da un # o ., si sta cercando letteralmente alla ricerca per il tag:

<<start feat="11" class="ng-scope"></start><annotation index="11" class="ng-isolate-scope ng-scope" style="background-color: rgb(238, 204, 153); background-position: initial initial; background-repeat: initial initial;">> 

ecc ...

Ecco una demo di ciò che intendo: http://jsfiddle.net/hpNN3/7/


semplicemente effettuare le seguenti operazioni:

var html = $('pre'); 

DEMO: http://jsfiddle.net/hpNN3/6/

+0

ok - ma questo li scava solo se sono nel DOM. Non voglio manipolare direttamente il DOM: voglio creare un oggetto (non legato al documento) e fare la mia trasformazione lì. –