JQuery Learning(4):JQuery events

本文详细介绍了jQuery中事件处理的基本概念、方法和实践,包括如何将代码整合到HTML头部,解决名称冲突,以及常见事件函数的使用。通过实例展示了如何隐藏页面中的段落元素,并强调了将jQuery功能封装到单独文件中的好处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JQuery is specially designed for the event handler.

JQuery event function

JQuery event handling is core functions in jQuery.

The event handler is called when some event occurs in the HTML.The terminology "trigger" will often be used.

Will usually be the jQuery code into <head> part of the event handler method:

Example:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>

</html>


In the above example,When the button click event is triggered will be call a function:

$("button").click(function() {..some code... } )


The method hides all the <p> elements:

$("p").hide();

Function in a separate file

If your web site contains a lot of pages,and you hope your jQuery functions easy to maintain,So please put your jQuery functions into the separate file.

When we demonstrate the jQuery in the tutorial,will be the function added to the <head> section,however,the best put it into a separate file, like this(via the src attribute to reference the file ):

Example:

<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_jquery_functions.js"></script>
</head>


JQuery name conflicts

The jQuery use the $ symbol as a jQuery short way.

Some other functions same use $ symbol in the  javascript library.

The jQuery use name is noConflict() function to resolve this issue.

var jq=jQuery.noConflict()help you use yourself name (such as jq) to replace $ symbol.

JQuery events

The following jQuery event example:

Event  functionsbinding the functions to
$(document).ready(function)bind a function to the document ready event
$(selector).click(function)Trigger or bind a function to the click event of selected element.
$(selector).dblclick(function)Trigger or bind a function to the double click event of selected element.
$(selector).focus(function)Trigger or bind a function to the get focus event of selected element.
$(selector).mouseover(function)Trigger or bind a function to the mouse over event of selected element.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值