简体   繁体   English

如何将 class 应用于在 p5.js 中创建的 P 标签?

[英]How can I apply a class to a P tag created in p5.js?

I am generating an element using p5.js.我正在使用 p5.js 生成一个元素。

How can I apply the basic_font class to this p element?如何将basic_font class 应用于此p元素?

*.js (p5.js) *.js (p5.js)

createElement('p', "Number of columns:").parent(div);

*.css *.css

.basic_font {
    color: white;
    font-size: larger;
    font-family: 'Lato', sans-serif;
    justify-content: center;
}

You can use classList to add a class (in this case basic_font) and its styles to your element.您可以使用classList将 class(在本例中为 basic_font)及其 styles 添加到您的元素。 parent is the parent element. parent 是父元素。

 var el = document.createElement('p'); el.innerHTML = 'Number of Columns'; el.classList.add('basic_font'); parent.appendChild(el);

See https://developer.mozilla.org/en-US/docs/Web/API/Element/classList请参阅https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

You can also use setAttribute on class .您还可以在class上使用 setAttribute。

el.setAttribute('class', 'basic_font');

Keep in mind also that the way you added innerHTML to the element is incorrect.还要记住,您将 innerHTML 添加到元素的方式是不正确的。 You must specify it separately.您必须单独指定它。

You mentioned you are using p5.js which appears to have a class() method for adding classes to created elements.您提到您正在使用 p5.js,它似乎有一个class()方法用于将类添加到创建的元素。 See https://p5js.org/reference/#/p5.Element .请参阅https://p5js.org/reference/#/p5.Element Try using it as follows:尝试如下使用它:

createElement('p', "Number of columns:").parent(div).class('basic_font');

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

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