刷新 DIV 容器


Refresh DIV container

我编写了代码,以便在执行函数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>

试试这个

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 .

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

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

你可以试试这些: -

<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在做代码时检查语法错误。