Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

javascript - 如何在文本前添加元素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(我的意思是在此标签之前选择文本-并将其放在自己的元素内)   ask by MSDesCUC translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this will look for every single a element and do what you want to it and remove is ext from it as well(这将查找每个单个元素,并执行您想要的操作,并从该元素中移除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); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...