开发者

Trying to replace contents of a Div, with no luck

Ive tried to use the Dom model with no bloody luck, getElementByID just doesnt work for me.

I loathe to resort to a regex but not sure what else to do.

The idea is to replace a <div id="content_div"> all sorts </div> with a new <div id="content_div"> NEW ALL SORTS HERE </div> and keep anything that was before or after it in the string.

The string is a partial HTML string and more specifically开发者_JS百科 out of the wordpress Posts DB.

Any ideas?

UPDATE: I tagged this question PHP but probably should of mentioned Im looking for a PHP solution only.

Update: Code Example

$content = ($wpdb->get_var( "SELECT `post_content` FROM $wpdb->posts WHERE ID = {$article[post_id]}" ));

$doc = new DOMDocument();
$doc->validateOnParse = true; 
$doc->loadHTMLFile($content);   
$element = $doc->getElementById('div_to_edit');

So Ive tried a whole lot of code and this is what Ive got so far, probably not right but Ive been hacking at it for a little while now.


You're right: getElementByID doesn't work.

Try getElementById() instead. Javascript is case sensitive.


ok i assume $content is a snippet? then this may be all you need:

$doc = new DOMDocument();
$doc->validateOnParse = true; 
$doc->loadHTMLFile('<html><body>' . $content . '</body></html>');   
$element = $doc->getElementById('div_to_edit'); 


remember that you must have a valid webpage and that means full HTML tree:

<html>
<head>
<script type="text/javascript">
function changeText(div){
    document.getElementById(div).innerHTML = 'my new text';
}
</script>
</head>
<body>

--- your body ---

<div id="DIV_ID">old text</div>

<input type="button" onclick="chageText('DIV_ID');" value="Click me" />

</body>
</html>

remember that you have to call getElementById with:

<script type="text/javascript"> // as from HTML5 you can use only <script>
    document.getElementById("DIV_ID").innerHTML = 'my new text';
</script>


Try this..

var myDiv = document.getElementById('content_div');
myDiv.innerHTML = "NEW ALL SORTS HERE";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜