最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

font awesome - JavaScript element.classList.add("fa fa-hand-rock-o") Error: "String contains an inval

programmeradmin1浏览0评论

I'm trying to add a class to <span id="sp1"> using:

document.getElementById("sp1").classList.add("fa fa-hand-rock-o");

But it is showing error:

String contains an invalid character

I'm trying to add a class to <span id="sp1"> using:

document.getElementById("sp1").classList.add("fa fa-hand-rock-o");

But it is showing error:

String contains an invalid character

Share Improve this question edited May 28, 2018 at 8:22 Learner AKS asked Mar 18, 2018 at 14:15 Learner AKSLearner AKS 1831 gold badge3 silver badges10 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 11

fa fa-hand-rock-o can not be a single class because class names can not have space(s).

Here I assume you are trying to add two different classes. When adding multiple classes by using classList.add() specify all the classes as individual ma separated string like:

.add("fa", "fa-hand-rock-o")

Code Example:

document.getElementById("sp1").classList.add("fa","fa-hand-rock-o");

console.log(document.getElementById("sp1"));
<span id="sp1">Test Container</span >

The white space between two class name is creating the issue.If need to add multiple class separate them by a ma and put each of them inside a quote

document.getElementById("sp1").classList.add("fa", "fa-hand-rock-o")
.fa {
  color: red;
}

.fa-hand-rock-o {
  font-size: 18px;
}
<li id="sp1"> Test Text </li>

发布评论

评论列表(0)

  1. 暂无评论