<script type="text/javascript">
function ButtonClick() {
var ps = document.getElementsByTagName("p");
for (var i = 0; i < ps.length; i++) {
if (ps[i].getElementsByTagName("img").length > 0) // <p>中有<img>
ps[i].align = "center";
}
}
</script>
<div id="div">
<input type="button" value="居中分页" onclick="ButtonClick();">
<p><img src="yiyi.png" width="100" height="100" ></p>
<p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定</p><p><img src="yiyi.png" width="100" height="100"></p>
<p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定</p><p><img src="yiyi.png" width="100" height="100"></p>
<p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定</p>
</div>

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head><body>
    <div id="div"> 
    <input type="button" value="居中分页" onclick="fun()" /> 
    <p> <img  src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" /> </p> 
    <p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定 </p> <p> <img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" /> </p> 
    <p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定 </p> <p> <img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" /> </p> 
    <p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定 </p> 
    </div> <script type="text/javascript">
    /*兼容的在节点位置插入HTML代码的函数
    *where:插入在哪里,BeforeBegin表示插入在标签之前,AfterBegin表示插入在标签之后,BeforeEnd表示在标签结束前,AfterEnd表示在标签结束后
    * el:需要操作的节点
    * html:需要插入的HTML代码
    */
    function insertHtml(where, el, html)
    {
    where = where.toLowerCase();
        //IE
    if(el.insertAdjacentHTML)
    {
    switch(where)
    {
                case "beforebegin":
                    el.insertAdjacentHTML('BeforeBegin', html);
                    return el.previousSibling;
                case "afterbegin":
                    el.insertAdjacentHTML('AfterBegin', html);
                    return el.firstChild;
                case "beforeend":
                    el.insertAdjacentHTML('BeforeEnd', html);
                    return el.lastChild;
                case "afterend":
                    el.insertAdjacentHTML('AfterEnd', html);
                    return el.nextSibling;
            }
            throw '无效的位置"' + where + '"';
    }
        //Fifefox
    var range = el.ownerDocument.createRange();
        var frag;
    switch(where)
    {
    case "beforebegin":
                range.setStartBefore(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el);
                return el.previousSibling;
    case "afterbegin":
                if(el.firstChild){
                    range.setStartBefore(el.firstChild);
                    frag = range.createContextualFragment(html);
                    el.insertBefore(frag, el.firstChild);
                    return el.firstChild;
                }else{
                    el.innerHTML = html;
                    return el.firstChild;
                }
    case "beforeend":
                if(el.lastChild){
                    range.setStartAfter(el.lastChild);
                    frag = range.createContextualFragment(html);
                    el.appendChild(frag);
                    return el.lastChild;
                }else{
                    el.innerHTML = html;
                    return el.lastChild;
                }
    case "afterend":
                range.setStartAfter(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el.nextSibling);
                return el.nextSibling;
    }
            throw '无效的位置"' + where + '"';
    }function fun()
    {
    var p= document.getElementById("div").getElementsByTagName("p");
    for(var i=0;i<p.length;i++)
    {
    if(i%2==0)
    p[i].align = "center";
    else
    {
    if(i!=p.length-1)
    insertHtml("AfterEnd", p[i], "<hr/>")
    }
    }
    }
    </script>
    </body>
    </html>
      

  2.   

    根据1楼的代码改了点东西
    <script type="text/javascript">
    function ButtonClick() {
    var ps = document.getElementsByTagName("p");
    for (var i = 0; i < ps.length; i++) {
    if (ps[i].getElementsByTagName("img").length > 0) {
    ps[i].align = "center";
    if( i > 0 )
    ps[i].parentNode.insertBefore(document.createElement("hr"), ps[i]);

    }
    }
    }
    </script>
    <div id="div"><input type="button" value="居中分页" onclick="ButtonClick();">
    <p><img src="yiyi.png" width="100" height="100" ></p>
    <p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定</p>
    <p><img src="yiyi.png" width="100" height="100"></p>
    <p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定</p>
    <p><img src="yiyi.png" width="100" height="100"></p>
    <p>一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定能一定</p>
    </div>