如何删除重复的attr href,但保留第一个Javascript或jQuery?
以下是默认代码。 a.class
和id
无法更改。
<div class="test-class">
<a href="test-0" class="link">Text0</a>
<a href="test-1" class="link">Text1</a>
<a href="test-1" class="link">Text1</a>
<a href="test-2" class="link">Text2</a>
<a href="test-2" class="link">Text2</a>
<a href="test-3" class="link">Text3</a>
<a href="test-3" class="link">Text3</a>
<a href="test-3" class="link">Text3</a>
</div>
我需要这个:
<div class="test-class">
<a href="test-0" class="link">Text0</a>
<a href="test-1" class="link">Text1</a>
Text1
<a href="test-2" class="link">Text2</a>
Text2
<a href="test-3" class="link">Text3</a>
Text3
Text3
</div>
答案 0 :(得分:1)
这样的事情大概就是你要找的东西:
var sketchProc = function (processing) {
processing.size(400, 400);
processing.frameRate(30);
}
var canvas = $('#game'); // use jQuery since you have it
var processingInstance = new Processing(canvas, sketchProc);
// game code now manipulates `processingInstance`
// either directly in scope (or even better as object with parameter)
processingInstance.draw = function () {
var processing = this;
// if your code originally used `processing` throughout,
// do this so that it refers to the specific instance you've created
// ...
}
processingInstance.loop();
// if initialized without a `.draw()` method like above, you need to start the loop manually once there is one
&#13;
let tracker = {}; // track href values
$('a').each(function(anc) {
var href = $(this).attr('href');
if (tracker.hasOwnProperty(href)) {
$(this).replaceWith(`<div>${$(this).text()}</div>`);
} else {
tracker[href] = true;
}
});
&#13;
答案 1 :(得分:0)
您可以尝试使用以下javascript:
var hrefTags = document.getElementsByTagName('A');
var arrValue = [];
for (var i=0; i<hrefTags.length; i++){
if(arrValue.indexOf(hrefTags[i].innerHTML)>-1){
hrefTags[i].outerHTML = hrefTags[i].outerHTML.replace(/a/g, 'span');
i--;
} else {
arrValue.push(hrefTags[i].innerHTML);
}
}
&#13;
<div class="test-class">
<a href="test-0" class="link">Text0</a>
<a href="test-1" class="link">Text1</a>
<a href="test-1" class="link">Text1</a>
<a href="test-2" class="link">Text2</a>
<a href="test-2" class="link">Text2</a>
<a href="test-3" class="link">Text3</a>
<a href="test-3" class="link">Text3</a>
<a href="test-3" class="link">Text3</a>
</div>
&#13;
答案 2 :(得分:0)
很抱歉迟到的回复,但这是我的答案:D
$(window).on('load',function() {
var last_elm = $('a[href]:first').text();
$('a[href]').each(function(i) {
var content = $(this).text();
if (i && content==last_elm) $(this).replaceWith('<span>'+content+'</span>');
last_elm=content;
});
});