简体   繁体   English

如何在文本前添加元素 div(如何在元素内附加文本)

[英]How to add element div before text (How to append text within an element)

Hello I have a element <a href='#'>whatsapp-300</a> and i want select before this mark - and make it like this <i class='fa fa-whatsapp'></i> knowing that the word whatsapp is not fixed and can be facebook and others .. i want a way to do this either by JavaScript or jQuery .. and thank you :D你好,我有一个<a href='#'>whatsapp-300</a>元素,我想在这个标记之前选择-并且让它像这样<i class='fa fa-whatsapp'></i>知道这一点whatsapp这个词不是固定的,可以是facebook和其他人..我想要一种通过JavaScript或jQuery做到这一点的方法..谢谢:D

What I mean is to pick the text before this tag - and put it inside its own element我的意思是在这个标签之前选择文本-并将它放在它自己的元素中

this will look for every single a element and do what you want to it and remove is ext from it as well这将查找每个元素并执行您想要的操作并从中删除 is ext

$('a').each(function(){
        let text=$(this).clone().children().remove().end().text();
        let name=text.split("-");
        let logo=document.createElement("i");
        logo.classList += "fa fa-"+name[0];
        $(this).text("");
        $(this).append(logo);
    });

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

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