当你单击某个元素时, 将发生click事件, 一旦单击事件发生, 它将执行click()方法或附加一个要运行的函数。
它通常与jQuery的其他事件一起使用。
句法:
$(selector).click()
它用于触发所选元素的click事件。
$(selector).click(function)
用于将功能附加到click事件。
让我们以一个示例来演示jQuery click()事件。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
alert("This paragraph was clicked.");
});
});
</script>
</head>
<body>
<p>Click on the statement.</p>
</body>
</html>
立即测试
输出:
单击该语句。
让我们以一个示例来演示jquery click()事件。在此示例中, 当你单击标题元素时, 它将隐藏当前标题。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("h1, h2, h3").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h1>This heading will disappear if you click on this.</h1>
<h2>I will also disappear.</h2>
<h3>Me too.</h3>
</body>
</html>
立即测试
输出:
评论前必须登录!
注册