Javascript 如何向HTML元素添加选择器?

Javascript 如何向HTML元素添加选择器?,javascript,Javascript,我有一个跨度元素: let infoButton = document.createElement("span"); infoButton.setAttribute("class", "info-point"); 如何将选择器[bold]添加到元素中,以获得以下信息: <span [bold] class="info-point"></span> 或 使用空字符串设置属性应该会得到这样的结果。这称为布尔属性,通常用于React、Angular等框架中。它们在中定义为

我有一个跨度元素:

let infoButton = document.createElement("span"); 
infoButton.setAttribute("class", "info-point");
如何将选择器[bold]添加到元素中,以获得以下信息:

<span [bold] class="info-point"></span>


使用空字符串设置属性应该会得到这样的结果。这称为布尔属性,通常用于React、Angular等框架中。它们在中定义为有效

让infoButton=document.createElementspan; infoButton.setAttributeclass,信息点; infoButton.setAttributebold;
console.loginfoButton 最好使用本机API来管理元素类

var infoButton = document.createElement("span"); 

infoButton.classList.add("info-point");

您可以使用以下指令生成粗体文本

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[boldText]'
})
export class BoldTextDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.fontWeight = 'bold';
    }
}
HTML


希望,这会有帮助。

不,我需要,正如我所说的,这是给你的directive@vy32这些是布尔属性,在HTML5/4规范中是有效的。它是angularjs,我相信你可能会从中了解到。我使用Angular 8这是正确的,你应该使用classList API来处理类,错误的是,所讨论的属性不是类,这是一种属性
import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[boldText]'
})
export class BoldTextDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.fontWeight = 'bold';
    }
}
<span boldText class="info-point"></span>