0
<!DOCTYPE html>
<html>

<head>

<script type="text/JavaScript" src="jquery.min.js"></script>

</head>

<body>

<div> Old Content </div>

<script>
$(document).ready(function(){

$("button").click(hidefunction());
});

function hidefunction(){

// Hide all divs on the page
$("div").hide();

}
</script>

<div> New Content. If you click on me, I will disappear.This jquery code doesn't work if we takeout document.ready code from function. The Function "hidefunction" works by itself. Without clicking the button the "div" hides.  </div>
<button> Hide all div</button>

</body>

</html> 
4

2 回答 2

3

您正在调用 hidefunction 而不是将引用传递给该函数。尝试这个:

$("button").click(hidefunction);
于 2013-10-10T18:39:19.073 回答
0

当你这样做时

$("button").click(hidefunction());

您正在调用hidefunction,如果您想通过参数传递函数,请不要添加()

$("button").click(hidefunction);
于 2013-10-10T18:39:52.013 回答