简体   繁体   English

使用 JavaScript 插入字体真棒图标

[英]Inserting font awesome icon using JavaScript

I am working on a WordPress site using a jobs application plugin.我正在使用工作应用程序插件在 WordPress 网站上工作。 I want to add a font awesome icon to a button using javascript.我想使用javascript向按钮添加一个字体真棒图标。 The hml code I can see for the button is.我可以看到按钮的 hml 代码是。

<input type = "button" class="application_button button" value="apply for job"></input>

which I think I need to make the following for the icon to show.我认为我需要为图标显示以下内容。

<input type = "button" class="application_button button" value="apply for job"> <i class = "fas fa-chevron-right"></i></input>

the JavaScript I have added to achieve this is,我为实现这一目标而添加的 JavaScript 是,

 var d1 = document.querySelector('.application_button');
d1.insertAdjacentHTML('beforeend', '<i class="fas fa-chevron-right"></i>');

When I console.log(d1);当我 console.log(d1); I can see that I have got the code I want.我可以看到我得到了我想要的代码。 But the icon does not show.但是图标不显示。 If I use 'afterend' then I do see the icon, but outside the button.如果我使用“afterend”,那么我会看到图标,但在按钮之外。

I seem so close, but so far away.我看起来很近,但又很远。

Any advice?有什么建议吗?

Font awesome icons rely on creating their icons on a pseudo element ::before , which creates the icon via that pseudo element's content property.字体真棒图标依赖于在伪元素::before上创建它们的图标,它通过该伪元素的content属性创建图标。

input tags cannot have content , thus using font awesome icons on them cannot work. input标签不能有content ,因此在它们上使用字体真棒图标无法工作。

Instead of代替

<input type="button" class="application_button button" value="apply for job"></input>

which, btw, is invalid HTML since input cannot have a closing tag </input> for the same reason ::before doesn't work on it, use顺便说一句,这是无效的 HTML,因为input不能有结束标记</input>出于同样的原因::before对它不起作用,请使用

<button type="button" class="application_button button">apply for job</button>

Since you need the icon at the right side of the button, you can do that either by adding an element at the end that carries the CSS classes fas fa-chevron-right :由于您需要按钮右侧的图标,您可以通过在末尾添加一个带有 CSS 类fas fa-chevron-right的元素来实现:

 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" rel="stylesheet"/> <button type="button" class="application_button button">apply for job <i class="fas fa-chevron-right"></i></button>

or by adding this simple CSS:或者通过添加这个简单的 CSS:

 .application_button.button.fas.fa-chevron-right::before { float: right; margin-left: 5px; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" rel="stylesheet"/> <button type="button" class="application_button button fas fa-chevron-right"> apply for job</button>

Which method you use is up to you, I personally prefer to solve layout-related problems with CSS rather than applying otherwise unnecessary additional markup.您使用哪种方法取决于您,我个人更喜欢使用 CSS 解决与布局相关的问题,而不是应用其他不必要的附加标记。

As I said in comments:正如我在评论中所说:

INPUT is a void element (like <br> etc...). INPUT 是一个空元素(如<br>等...)。 Closing it with </input> is invalid markup .</input>关闭它是无效标记

Therefore, use a BUTTON element因此,使用 BUTTON 元素

 <button type="button">Apply for job <i class="fas fa-chevron-right"></i></button>

 <button type="button"><i class="fas fa-chevron-left"></i> Go back</button>

Using an icon inside the Value of an INPUT在 INPUT 的值中使用图标

JavaScript solution JavaScript 解决方案

If you really cannot change the markup from <input> to <button> element, than you could define the font-family with the desired fallbacks right in CSS, and concatenate the current INPUT value to the unicode representation of the Font Awesome icon el.value += " \" :如果您确实无法将标记从<input>更改为<button>元素,那么您可以在 CSS 中定义具有所需回退的font-family ,并将当前 INPUT 值连接到 Font Awesome 图标el.value += " \"unicode 表示el.value += " \"

Example using both Google fonts and Font Awesome :使用Google 字体和 Font Awesome 的示例:

 const addChevron = el => el.value += " \"; document.querySelectorAll('input.button').forEach(addChevron);
 .button { font-family: 'Montserrat', 'Font Awesome 5 Free', sans-serif; font-weight: 900; /* Needed for font awesome to work... */ }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" rel="stylesheet"/> <link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" rel="stylesheet"> <input class="button" type="button" value="Submit to win!">

If you use some Google font, or you want to inherit the font, no worries, pick one:如果你使用一些谷歌字体,或者你想继承字体,不用担心,选择一个:

font-family: 'Some Google Font', 'Font Awesome 5 Free', sans-serif;
font-family: inherit, 'Font Awesome 5 Free', sans-serif;
font-family: 'Font Awesome 5 Free', sans-serif;

you get the idea.你明白了。


HTML solution HTML 解决方案

If you don't want to use JavaScript than you could use the unicode representation right inside the value attribute: &#xf054;如果您不想使用 JavaScript,则可以在value属性中使用 unicode 表示: &#xf054;

 .button { font-family: 'Font Awesome 5 Free', sans-serif; font-weight: 900; /* Needed for font awesome to work... */ }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" rel="stylesheet"/> <input class="button" type="button" value="Submit to win! &#xf054;">

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

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