我试图在用户单击网页上的元素时创建一些功能。页面执行完毕后,回调函数就会执行。它只应该在用户单击某个元素时执行。 这是代码:
<!DOCTYPE html>
<html>
<head>
<title>Javascript Test</title>
<script src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>
$("#clickMe").one('click', printThis("Hello All"));
function printThis(msg) {
console.log(msg);
}
</script>
</head>
<body>
<div id="clickMe">Click me!</div>
</body>
</html>
谢谢!
最佳答案
这实际上并不是传递函数,而是评估函数并将结果作为回调参数传递(在本例中为 undefined
)。
试试这个
<script>
function printThis(msg) {
console.log(msg);
}
$("#clickMe").one('click', function() {
printThis("Hello All");
});
</script>
关于javascript - 尝试传入回调函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811746/