刷新DIV容器

时间:2012-05-25 09:13:25

标签: php javascript ajax

执行函数click_function_ps()后,我编写了代码来刷新DIV容器。但我的代码不起作用。它说:

missing ; before statement
var newHTML = "<img class="displayed" src="ganttchart.php">";

那么,我需要在哪里放;

<div class="buttons">
    <a href="#" class="regular" onclick="click_function_ps(); replaceContent();">
        <img src="images/opt.png" alt=""/> Run </a>
</div>

<div id="opt_container">
        <table width="100%">
            <tr>
                <td width="100%">
                    <div class="scrollbar" id="chart">
                        <img class="displayed" src="ganttchart.php">
                    </div>  
                </td>
            </tr>
        </table>
</div>


<script type="text/javascript">
function replaceContent() {
     var newHTML = "<img class="displayed" src="ganttchart.php">";
     document.getElementById("scrollbar").innerHTML = newHTML;
}
</script>

3 个答案:

答案 0 :(得分:3)

试试这个

var newHTML = "<img class=\"displayed\" src=\"ganttchart.php\">";

此外,没有ID scrollbar的元素。要么将DIV从class="scrollbar"更改为id="scrollbar",要么立即使用ID chart,或者按照document.getElementsByClassName("scrollbar")[0]替换javascript。但请记住,IE不支持getElementsByClassName()。我强烈建议您使用id代替class

答案 1 :(得分:1)

你没有逃避这部分的双引号:

function replaceContent() {
   var newHTML = "<img class=\"displayed\" src=\"ganttchart.php\">";
   document.getElementById("scrollbar").innerHTML = newHTML;
}

答案 2 :(得分:0)

您可以尝试以下方法: -

<div class="buttons">
    <a href="#" class="regular" id="scrollbar" onclick="click_function_ps(); replaceContent();">
        <img src="images/opt.png" alt=""/> Run </a>
</div>

<div id="opt_container">
        <table width="100%">
            <tr>
                <td width="100%">
                    <div class="scrollbar" id="chart">
                        <img class="displayed" src="ganttchart.php">
                    </div>  
                </td>
            </tr>
        </table>
</div>


<script type="text/javascript">
function replaceContent() {
     var newHTML = "<img class=\"displayed\" src=\"ganttchart.php\">";
     document.getElementById("scrollbar").innerHTML = newHTML;
}
</script>

如果您使用任何IDE(netbeans etc),则不会出现此类错误。 因为IDE会在您执行代码时检查语法错误。