2013-07-04 18 views
5

Mam HTML z kilkoma niestandardowymi tagami. Chcę znaleźć wszystkie oprócz dwóch ("start", "koniec") i odwijaj je. jQuery.find() wydaje się znajdować tylko te niestandardowe znaczniki, kiedy przeszukuję to, co jest w dokumencie, a nie kiedy przeszukuję obiekt jQuery. Co ja robię źle?jQuery nie znajdzie niestandardowych tagów

Powinno być oczywiste na skrzypcach:

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

Oto część 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) 

i przykładem 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> 

który powinien być:

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

Dzięki

+4

html z niestandardowych znaczników html –

+1

nie jest z wyjątkiem

+2

Po co pisać HTML w ten sposób? Jakie korzyści ma tag '' mieć ponad '

' lub ''? Nie wspominając, że szybciej jest użyć selektora '$ ('. ClassName')'. – Dom

Odpowiedz

3

Twój problem leży w początkowych zmiennych:

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

To przekłada się var html = $($('pre').html()), który nie będzie pasował do żadnego elementu. Wynika to z faktu, że od selektor nie jest poprzedzone # lub ., to szuka dosłownie szuka tagu:

<<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;">> 

etc ...

Oto demo o co mi chodzi: http://jsfiddle.net/hpNN3/7/


Wystarczy wykonać następujące czynności:

var html = $('pre'); 

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

+0

ok - ale to tylko rozpakowuje je, jeśli są w DOM. Nie chcę bezpośrednio manipulować DOM - chcę zrobić obiekt (nie związany z dokumentem) i przeprowadzić tam moją transformację. –

Powiązane problemy