javascript - 按钮不调用脚本 - onclick 方法

标签 javascript php jquery html

由于某种原因,我的按钮没有调用脚本,并且 Chrome 的控制台回复错误“意外的 token }”

这是脚本:

  <script type="text/javascript" src="js/jquery-1.10.2.min.js">

  function printContent(el){
    var restorepage = document.body.innerHTML;
    var printcontent = document.getElementById(el).innerHTML;
    document.body.innderHTML = printcontent;
    window.print();
    document.body.innderHTML = restorepage;
  }

</script>

这是我的按钮:

echo '<center>
       <button onclick="printContent("printable");" class="btn btn-default">
        <span class="glyphicon glyphicon-print"></span> &nbsp; Print
       </button>
      </center> ';

出现 echo 的原因是我使用PhP根据情况调用按钮。

它应该打印的 div 看起来像这样:

<div class="container" id="printable">

最佳答案

您需要关闭 <script>导入 jQuery 并创建新的 <script> 的标签您可以在其中放置自己的 JavaScript 标记,如下所示:

<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script>
  function printContent(el) {
    var restorepage = document.body.innerHTML;
    var printcontent = document.getElementById(el).innerHTML;
    document.body.innerHTML = printcontent;
    window.print();
    document.body.innerHTML = restorepage;
  }
</script>

如前所述,您在输入 innerHTML 时犯了拼写错误,我在上面的代码中修复了它,并且出现错误,因为您在 onclick 中使用了双引号两次,您可以通过更改 echo 来修复此问题对此:

echo '<center>
       <button onclick="printContent(\'printable\');" class="btn btn-default">
        <span class="glyphicon glyphicon-print"></span> &nbsp; Print
       </button>
      </center> ';

关于javascript - 按钮不调用脚本 - onclick 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37668105/

相关文章:

javascript - 如何使用 JavaScript 使鼠标悬停时图像变大?

javascript - 悬停 2 秒时显示下拉菜单

javascript - 如何在不点击popup的情况下将数据从popup.html发送到content_script

javascript - a.toLowerCase 将 CORS 添加到 ajax header 时

javascript - Jquery:+ 不能正常工作

javascript - 正则表达式:如何在文本之间获取某些内容,但其他文本之间除外

javascript - for 循环中的嵌套 Promise 的行为不符合预期

php - paypal支付成功后重定向到一个页面

php - 逻辑和物理 URL

php - 未捕获的异常 'Google_AuthException',消息为 'Could not json decode the token'