当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


p5.js Element removeClass()用法及代码示例


p5.j​​s中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

相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 p5.js Element removeClass() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。