javascript - jQuery 找不到自定义标签

标签 javascript jquery html

我有带有几个自定义标签的 HTML。我想找到除两个(“开始”、“结束”)以外的所有内容并打开它们。 jQuery.find() 似乎只在我搜索文档中的内容时找到这些自定义标签,而不是在我搜索 jQuery 对象时。我究竟做错了什么?

在 fiddle 中应该是不言自明的:

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

这是 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)

和 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>

应该是:

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

谢谢

最佳答案

您的问题在于您的初始变量:

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

这转换为 var html = $($('pre').html()),它不会匹配任何元素。原因在于,由于选择器前面没有 #.,所以它看起来literally对于标签:

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

等...

这是我的意思的演示: http://jsfiddle.net/hpNN3/7/


只需执行以下操作:

var html = $('pre');

演示: http://jsfiddle.net/hpNN3/6/

关于javascript - jQuery 找不到自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460067/

相关文章:

java - 为什么通知栏不起作用?

javascript - 带点滚动的 sidenav

jquery - 火力地堡错误 : Messaging: We are unable to register the default service worker

javascript - A-Frame 在与相机发生碰撞时触发 javascript 函数

php - 如何查询简单机械论坛的主题流行度/评级

javascript - 从生成的数字中选择一个变量

javascript - 为什么在使用 ng-blur 时 ng-click 不起作用?

jQuery 插件在两个窗口中显示两个文件之间的差异

javascript - 使用 getUserMedia 从多个网络摄像头捕获视频

javascript - 显示带有来自 json 的真实值的复选框