遍历每个锚点,如果它是特定的其他东西,则替换内部html

Travel through every anchor and replace inner-HTML if it is something with a specific something-else

本文关键字:其他 html 内部 替换 如果 遍历      更新时间:2023-09-26

我正在尝试遍历类nav (.nav)的所有子类锚并检查它们的内容。如果是上述任何一种情况,则将其内容替换为特定的和相应的unicode。

为什么这不起作用,有没有比我尝试过的更好的方法?

$(document).ready(function(){
    var navigationLinks = $('.nav a');
    for(var i=0; i < navigationLinks.length; i++){
    var thisLink = navigationLinks[i];
    switch(thisLink.html()){
        case "About":
            thisLink.replace(/About/g,'&#xe00f;');
        case "Work":
            thisLink.replace(/Work/g,'&#xe010');
        case "CV":
            thisLink.replace(/CV/g,'&#xe00c');
        case "Resume":
            thisLink.replace(/Resume/g,'&#xe00d;');
        case "down":
            thisLink.replace(/down/g,'&#xe00d;');
        case "Mail":
            thisLink.replace(/Mail/g,'&#xe011;');
        case "Dribbble":
            thisLink.replace(/Dribbble/g,'&#xe015;');
        case "GooglePlus":
            thisLink.replace(/GooglePlus/g,'&#xe012;');
        case "Facebook":
            thisLink.replace(/Facebook/g,'&#xe013;');
        case "Twitter":
            thisLink.replace(/Twitter/g,'&#xe014');
        default:
            thisLink.replace(thisLink.html(),thisLink.html());
    }
}
});

使用方法:

$(document).ready(function(){
    var navigationLinks = $('.nav a');
    navigationLinks.each(function() {
    var thisLink = $(this);
    switch(thisLink.html()){
        // Your code with thisLink.html();
    }
}
});

您的代码绰绰有余。使用thisLink.html()来替换或修改

var thisLink = navigationLinks[i].html();
    switch(thisLink){
//
//
}