📜  p5.js 元素 removeClass() 方法

📅  最后修改于: 2022-05-13 01:56:31.534000             🧑  作者: Mango

p5.js 元素 removeClass() 方法

p5.js 中 p5.Element 的 removeClass ()方法用于将指定的类移除到一个元素中。一个元素可以有多个类分配给它。此外,可以为页面上的多个元素指定一个类。

句法:

removeClass(class)

参数:此函数接受如上所述和下文所述的单个参数。

  • class:它是一个字符串,表示要删除的类。

示例:下面的示例说明了 p5.js 中的 removeClass ()方法

Javascript
function setup() {
  createCanvas(600, 300);
  textSize(18);
  
  text("Click on the buttons to add, remove " + 
       "or show the classes of the elements", 20, 20);
  
  setBtn = 
    createButton("Add given class to element");
  setBtn.position(30, 40);
  setBtn.mouseClicked(addClass);
  
  removeBtn = 
    createButton("Remove given class from element");
  removeBtn.position(30, 70);
  removeBtn.mouseClicked(removeClass);
  
  showBtn = createButton("Show current classes");
  showBtn.position(30, 100);
  showBtn.mouseClicked(showClasses);
  
  class_name = createInput('class1');
  class_name.position(30, 140);
  
  // Create a new p5.Element
  tmpElement = createElement("div");
}
  
function addClass() {
  clear();
  
  // Get the class to set
  let classToSet = class_name.value();
  
  // Set the class of the element
  tmpElement.addClass(classToSet);
  
  text("Class added with name: " + 
       classToSet, 20, 200);
  
  text("Click on the buttons to add, remove or " +
       "show the classes of the elements", 20, 20);
}
  
function removeClass() {
  clear();
  
  // Get the class to remove
  let classToRemove = class_name.value();
  
  // Remove the class of the element
  tmpElement.removeClass(classToRemove);
  
  text("Class removed with name: " +
       classToRemove, 20, 200);
  
  text("Click on the buttons to add, remove or " +
       "show the classes of the elements", 20, 20);
}
  
function showClasses() {
  clear();
  
  // Get the classes of the element
  let setClasses = tmpElement.class();
  
  // Display the classes
  if (setClasses != '')
    text("The classes of the element are: " +
         setClasses, 20, 180);
  else
    text("The element has no classes", 20, 180);
  
  text("Click on the buttons to add, remove or " +
       "show the classes of the elements", 20, 20);
}


输出:

在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.Element/removeClass