由于某种原因,我的按钮没有调用脚本,并且 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> 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> Print
</button>
</center> ';
关于javascript - 按钮不调用脚本 - onclick 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37668105/