📜  p5.js parent()函数

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

p5.js parent()函数

parent()函数用于将元素附加为父元素。此函数接受字符串ID、DOM 节点或 p5.Element。如果不包含任何参数,此函数将返回父节点。

句法:

parent( parent )

或者

parent()

参数:它包含单个参数parent ,其中包含字符串p5.Element 对象作为元素。

下面的示例说明了 p5.js 中的 parent()函数:

例子:

function setup() {  
     
    // Create Canvas of given size 
    var cvs = createCanvas(600, 250);
}
  
function draw() {
    
    // Set the background color
    background('green'); 
    
    // Use createDiv() function to
    // create a div element
    var myDiv = createDiv('GeeksforGeeks');
    
    var myDiv1 = createDiv('A computer science portal for geeks');
    
    // Use parent() function
    myDiv1.parent(myDiv);
    
    // Set the position of div element
    myDiv.position(150, 100);  
    
    myDiv.style('text-align', 'center');
    
    // Set the font-size of text
    myDiv.style('font-size', '24px');
    
    // Set the font color
    myDiv.style('color', 'white');
  
}

输出: