Php:如何通过类添加样式输入元素 [英] Php: how to add style input element through class
问题描述
我有一个类Form,它有`write(),attr($ key,$ value)和addClass($ array)方法。 write()方法打印输入元素`type =$ this-> type,attr($ key,$ value)方法赋予该输入元素属性,addClass($ array)方法给出样式输入元素。我写了write()和attr($ key,$ value)方法,一切正常,它的工作,但我有一些addClass($ array)方法的问题。
例如`
$ f = new TextElement();
$ f-> attr(value,#97BEF0);
$ f-> addStyle(array(color=>red,font-size=>20px));
$ f-> write();
顺便说一下,我还有一个TextElement类,它继承自类表格。
我尝试过:
class Form {
public $ type;
public $ attr =;
public $ arr = array();
//打印输入
函数write(){
print< input type ='$ this-> type'$ this-> attr style ='$ this-> ARR'>中;
}
//添加属性
函数attr($ key,$ value){
$ this-> attr。=$ key ='$ value';
}
//添加样式
函数addStyle($ array){
for($ i = 0; $ i< count($ array); $ i ++){
$ this-> arr = $ array [$ i];
}
}
}
class TextElement extends Form {
function __construct(){
$ this-> type =text ;
}
}
$ p = new TextElement();
$ p-> attr(value,#97BEF0);
$ p-> addStyle(array(color=>red,font-size=>20px));
$ p-> write();
所以我的问题是使用addStyle($ array)方法,必须如何我给一个数组的参数?它给了我错误`Nitice:数组到字符串转换。
key,
value)和addClass(
array)方法。 write()方法打印输入元素`type =
I have a class Form, which has` write(), attr($key,$value) and addClass($array) methods. The write() method print an input element` type="$this->type", the attr($key,$value) method gives attribute to that input element, and the addClass($array) method gives style to that input element. I have written the write() and attr($key,$value) methods, everything is normal, it's work, but i have some problems with addClass($array) method.
For example`
$f = new TextElement(); $f->attr("value","#97BEF0"); $f->addStyle(array("color"=>"red", "font-size"=>"20px")); $f->write();
Btw, i also have a class TextElement, which is inherited from class Form.
What I have tried:
class Form{ public $type; public $attr = ""; public $arr = array(); //print input function write(){ print "<input type='$this->type' $this->attr style='$this->arr'>"; } //add attribute function attr($key,$value){ $this->attr .= "$key='$value'"; } //add style function addStyle($array){ for($i = 0; $i < count($array); $i++){ $this->arr = $array[$i]; } } } class TextElement extends Form{ function __construct(){ $this->type = "text"; } } $p = new TextElement(); $p->attr("value","#97BEF0"); $p->addStyle(array("color"=>"red", "font-size"=>"20px")); $p->write();
So my problem is with addStyle($array) method, How must i give parameters to an array? It gives me error` Nitice: Array to string conversion.
key,
value) and addClass(
array) methods. The write() method print an input element` type="
这篇关于Php:如何通过类添加样式输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!