2014-09-03 45 views
-1

我有我的代碼一些意見,這樣的:JS,評論之間標明內容

<!-- 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"} --> 

,我有錨這樣的:

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

這些鏈接的內容對應的評論「模板'元素,我想要實現的是當用戶將鼠標懸停在任何這些鏈接上時,它的對應內容(評論之間)將突出顯示。

我開始了一個簡單的jsfiddle例子http://jsfiddle.net/Banzay/md79aaby/,但不知道這是否可能。

對不起,如果我沒有解釋得很好。

Youssef

+0

所以基本上你希望能夠選擇在jQuery的意見? http://stackoverflow.com/questions/1623734/selecting-html-comments-with-jquery – 2014-09-03 14:45:05

回答

1

清理了我的上面的答案,並將其刪除。

你可能不得不改變你正在尋找評論的父母。正則表達式可以使用一些愛,但這是一般的想法。

HTML:

<!-- 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"} --> 
<a href="#one"> one </a> 
<br/> 
<a href="#two"> two </a> 

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 comment = getComment(templateName); 
    var element = $(comment).next(); 
    element.toggleClass('highlight'); 
}) 

http://jsfiddle.net/md79aaby/18/

+0

你真棒,非常感謝你,像一個魅力:) – Youssef 2014-09-03 15:29:17

+0

嗨對不起再次打擾你,我還有一件事,如果一個div沒有結束標記,你將如何獲得內容,請看看這裏:http://jsfiddle.net/Banzay/md79aaby/21/,正如你現在看到的,當我把它們全部改爲紅色,如果我試圖徘徊在兩三無所幸 – Youssef 2014-09-03 17:31:09