简体   繁体   English

获取带有开始和结束标签的注释之间的元素

[英]Get elements between comments that have an opening and a closing tags

I have a links that when hovered will highlight a specific content using comments in the page. 我有一个链接,将鼠标悬停时将使用页面中的注释突出显示特定内容。 Please have a look at this: 请看一下这个:

http://jsfiddle.net/Banzay/md79aaby/20/ http://jsfiddle.net/Banzay/md79aaby/20/

This is what I want to achieve and it is working fine, I have only one issue with it, my content will have an opening div tag that will be closed some where else and this is causing the jsfiddle above to not work properly, I have made a new fiddle and added the div that cause this to break, please have a look here 这是我想要实现的,并且运行正常,我只有一个问题,我的内容将带有一个打开的div标签,该标签将在其他地方关闭,这导致上述jsfiddle无法正常工作,我有做了一个新的小提琴并添加了导致此中断的div,请在这里看看

http://jsfiddle.net/Banzay/md79aaby/22/ http://jsfiddle.net/Banzay/md79aaby/22/

Here is the code that works fine before I add the div that cause the problem: 这是在添加导致问题的div之前可以正常工作的代码:

<!-- DebugDataTemplateBegin {"template":"one"} -->
    <div class="row notices" id="admin">comment One content</div>
<!-- DebugDataTemplateEnd {"template":"one"} -->
<!-- DebugDataTemplateBegin {"template":"two"} -->
    <div class="row notices" id="admin">comment Two content</div>
<!-- DebugDataTemplateEnd {"template":"two"} -->
<!-- DebugDataTemplateBegin {"template":"three"} -->
    <div class="row notices" id="admin">comment Three content</div>
<!-- DebugDataTemplateEnd {"template":"three"} -->
<a href="#one"> one </a> <br/>
<a href="#two"> two </a><br/>
<a href="#three">three</a>

This is the html with the div added: 这是添加了div的html:

<!-- DebugDataTemplateBegin {"template":"one"} -->

    <div> < ===========================

    <div class="row notices" id="admin">comment One content</div>
<!-- DebugDataTemplateEnd {"template":"one"} -->
<!-- DebugDataTemplateBegin {"template":"two"} -->
    <div class="row notices" id="admin">comment Two content</div>
<!-- DebugDataTemplateEnd {"template":"two"} -->
<!-- DebugDataTemplateBegin {"template":"three"} -->
    <div class="row notices" id="admin">comment Three content</div>
<!-- DebugDataTemplateEnd {"template":"three"} -->

</div> < ===================================

<a href="#one"> one </a> <br/>
<a href="#two"> two </a><br/>
<a href="#three">three</a>

JS code that I am using: 我正在使用的JS代码:

var getComment = function (templateName) {
    return $("body").contents().filter(function () {
        return this.nodeType == 8 && this.nodeValue.match(RegExp('DebugDataTemplateBegin.*'+templateName));
    })
}

$('a').hover(function () {
    // var templateName = this.href.split('#')[1];
    var templateName = $(this).text().trim();
    var comment = getComment(templateName);
    var element = $(comment).next();
    element.toggleClass('highlight');
})

CSS: CSS:

.highlight {
    color: red;
}

I hope I explained this well. 我希望我能解释得很好。

Youssef 优素福

Since you can't insert the comment of the template inside the div, you could try to make an if-else clause so you make it different for the first element: 由于您无法在div内插入模板的注释,因此可以尝试创建if-else子句,从而使第一个元素与之不同:

Your HTML: 您的HTML:

<!-- DebugDataTemplateBegin {"template":"one"} -->
<div>
    <div class="row notices" id="admin1">comment One content</div>
<!-- DebugDataTemplateEnd {"template":"one"} -->
<!-- DebugDataTemplateBegin {"template":"two"} -->
    <div class="row notices" id="admin2">comment Two content</div>
<!-- DebugDataTemplateEnd {"template":"two"} -->
<!-- DebugDataTemplateBegin {"template":"three"} -->
    <div class="row notices" id="admin3">comment Three content</div>
<!-- DebugDataTemplateEnd {"template":"three"} -->
</div>

The addapted JS: 新增的JS:

var getComment = function (templateName) {
if(templateName=="one"){
        //First element, search the comment normally.
        return $("body").contents().filter(function(){return this.nodeType == 8 && this.nodeValue.match(RegExp('DebugDataTemplateBegin.*'+templateName));  }); 
    }else{
        //Other elements, search comments inside the childrens of the body (the div)
    return $("body").children().contents().filter(function () {
        return this.nodeType == 8 && this.nodeValue.match(RegExp('DebugDataTemplateBegin.*'+templateName));
    });}
}

$('a').hover(function () {
    // var templateName = this.href.split('#')[1];
    var templateName = $(this).text().trim();
    var comment = getComment(templateName);
    if(templateName=="one"){
        //The first comment, look for the element inside the div
        var element = $(comment).next().children().first();
    }else{
        //Other comments, looks for them as normally
        var element = $(comment).next();
    }

    element.toggleClass('highlight');
})

Updated DEMO 更新了演示

Extra: You have different elements with the same ID, they should be different! 另外:您具有相同ID的不同元素,它们应该不同!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM