Max*_*tes 7 html javascript jquery
我有几个自定义标签的HTML.我想找到除了两个之外的所有('开始','结束')并打开它们.当我搜索文档中的内容时,jQuery.find()似乎只找到这些自定义标记,而不是在搜索jQuery对象时.我究竟做错了什么?
在小提琴中应该是不言自明的:
这是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)
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
应该是:
CTAGCTCTCTGGAGATTAACGAGGAGAAATACTAGAtTGGTTCAT<start feat="1" class="ng-scope"></start>GATCATAAgcttgaat<end feat="1" class="ng-scope"></end>tagccaaacttatt
谢谢
您的问题在于您的初始变量:
var raw = $('pre').html();
var html = $(raw);
Run Code Online (Sandbox Code Playgroud)
这将转换为var html = $($('pre').html()),它将不匹配任何元素。原因是,由于选择器前面没有#or .,所以它实际上是在寻找标签:
<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;">
</annotaion>
Run Code Online (Sandbox Code Playgroud)
ETC...
这是我的意思的演示: http://jsfiddle.net/hpNN3/7/
只需执行以下操作:
var html = $('pre');
Run Code Online (Sandbox Code Playgroud)
演示: http: //jsfiddle.net/hpNN3/6/