admin管理员组文章数量:1431398
I have the following button:
<input type="button" onclick="printDiv('print-content')" value="print a div!"/>
And the following JavaScript right below:
<script>
$(function() {
function printDiv(divName) {
alert('s')
var printContents = document.getElementById(divName).innerHTML
w=window.open()
w.document.write(printContents)
w.print()
w.close()
}
})
</script>
When I click printDiv, though I get the following error:
Uncaught ReferenceError: printDiv is not defined payment-history:398 onclick
I really don't get it.
I have the following button:
<input type="button" onclick="printDiv('print-content')" value="print a div!"/>
And the following JavaScript right below:
<script>
$(function() {
function printDiv(divName) {
alert('s')
var printContents = document.getElementById(divName).innerHTML
w=window.open()
w.document.write(printContents)
w.print()
w.close()
}
})
</script>
When I click printDiv, though I get the following error:
Uncaught ReferenceError: printDiv is not defined payment-history:398 onclick
I really don't get it.
Share Improve this question asked Apr 14, 2015 at 3:38 wycwyc 55.4k83 gold badges256 silver badges441 bronze badges 1- 1 I remend to read quirksmode/js/introevents.html if you want to learn more about event handlers. – Felix Kling Commented Apr 14, 2015 at 4:06
1 Answer
Reset to default 6Because printDiv
is not in global scope. You have wrapped it with a function. So, printDiv
scope is within that immediate function and not global.
JavaScript scoping and Hoisting
本文标签: javascriptWhy is my onclick event throwing undefinedStack Overflow
版权声明:本文标题:javascript - Why is my onclick event throwing undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745546677a2662735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论