[英]JavaScript element.classList.add(“fa fa-hand-rock-o”) Error: “String contains an invalid character”
I'm trying to add a class to <span id="sp1">
using: 我正在尝试使用以下方法向<span id="sp1">
添加一个类:
document.getElementById("sp1").classList.add("fa fa-hand-rock-o");
But it is showing error: 但它显示错误:
String contains an invalid character 字符串包含无效字符
fa fa-hand-rock-o
can not be a single class because class names can not have space(s). fa fa-hand-rock-o
不能是单个类,因为类名不能有空格。
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 comma separated string like: 使用classList.add()
添加多个类时,将所有类指定为单独的逗号分隔字符串,如:
.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 comma 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.