擅长:python、mysql、java
<p>为了获得最佳性能,请创建一个索引,然后重复使用:</p>
<pre><code>function revealCite() {
var cites_index = $("#myText").data("cites_index");
// if no cached index exists, prepare one (one-time hit code section)
if (!cites_index) {
var cites = $("#myNotes cite");
var cites_count = cites.length();
var cites_index = {};
for (var i=0; i<cites_count; i++) {
var cite = cites[i], group = cites_index[cite.className];
if (!group) cites_index[cite.className] = [];
group.push(cite);
}
$("#myText").data("cites_index", cites_index);
}
// use the index to work with related elements ("this" must be an <a> element)
$(cites_index[this.className]).show();
}
</code></pre>
<p>现在按您喜欢的任何方式触发上述函数:</p>
^{pr2}$
<p>PS:您也可以这样做,代替for循环:</p>
<pre><code>cites.each( function() {
var group = cites_index[this.className];
if (!group) cites_index[this.className] = [];
group.push(this);
});
</code></pre>
<p>但它的代码行数是相同的,而且可能有点慢。在</p>