PHP: Appending (adding) html content to exsisting element by ID

后端 未结 2 1843
误落风尘
误落风尘 2021-01-05 20:34

I need to search for an element by ID using PHP then appending html content to it. It seems simple enough but I\'m new to php and can\'t find the right function to use to d

2条回答
  •  攒了一身酷
    2021-01-05 21:05

    you can also append this way

    $html = '
    
        
            
    • hello
    • hello2
    • hello3
    • hello4
    '; libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->loadHTML($html); //get the element you want to append to $descBox = $doc->getElementById('one'); //create the element to append to #element1 $appended = $doc->createElement('li', 'This is a test element.'); //actually append the element $descBox->appendChild($appended); echo $doc->saveHTML();

    dont forget to saveHTML on the last line

提交回复
热议问题