0

I've the following code:

$newDOM = new DOMDocument('1.0', 'utf-8');
$foo = $newDOM->createElement('Input', 'THIS IS MY VALUE');
$newDOM->appendChild($foo);
echo $newDOM->saveHTML();

This is what is outputted only: <Input>

But when I change the createElement to a 'div' it works okay

This is what is outputted with 'div' or any other input:

 <div>THIS IS MY VALUE</div>

This is my var_dump($foo):

object(DOMElement)#5 (17) {
  ["tagName"]=>
  string(5) "Input"
  ["schemaTypeInfo"]=>
  NULL
  ["nodeName"]=>
  string(5) "Input"
  ["nodeValue"]=>
  string(16) "THIS IS MY VALUE"
  ["nodeType"]=>
  int(1)

  ["parentNode"]=>
  string(22) "(object value omitted)"
  ["childNodes"]=>
  string(22) "(object value omitted)"
  ["firstChild"]=>
  string(22) "(object value omitted)"
  ["lastChild"]=>
  string(22) "(object value omitted)"
  ["previousSibling"]=>
  NULL
  ["attributes"]=>
  string(22) "(object value omitted)"
  ["ownerDocument"]=>
  string(22) "(object value omitted)"
  ["namespaceURI"]=>
  NULL
  ["prefix"]=>
  string(0) ""
  ["localName"]=>
  string(5) "Input"
  ["baseURI"]=>
  NULL
  ["textContent"]=>
  string(16) "THIS IS MY VALUE"
}
4

1 回答 1

2

第二个参数createElement是您正在创建的标签的内容。<input>标签不能包含文本。你需要设置它的value属性。你正在尝试生产<input value="this is my value">,而不是<input>this is my value</input>

createAttribute

于 2013-08-07T16:56:23.110 回答