锋利的Jquery(p的onclick()事件)

1、一个p元素的点击事件

 1 <html xmlns="http://www.w3.org/1999/xhtml" >
 2 <head runat="server">
 3     <title>无标题页</title>
 4     <script type="text/javascript">
 5         function ClickMe() {
 6             alert(‘张晶是个粪堆‘);
 7         }
 8     </script>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13        <p onclick="ClickMe();">点击我</p>
14     </div>
15     </form>
16 </body>
17 </html>

2、所有p元素的点击事件

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head>
 3 <title></title>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <script type="text/javascript">
 6 window.onload = function(){//页面所有元素加载完毕
 7     var items = document.getElementsByTagName("p");//获取页面中的所有p元素
 8     for(var i=0;i < items.length;i++){    //循环
 9         items[i].onclick = function(){  //给每一个p添加onclick事件
10             //doing something...
11             alert("suc!");
12         }
13     }
14 }
15 </script>
16 </head>
17 <body>
18 <p>测试1</p>
19 <p>测试2</p>
20 </body>
21 </html>

3、带有循环的

 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head>
 3 <title></title>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <script type="text/javascript">
 6 window.onload = function(){//页面所有元素加载完毕
 7     var items = document.getElementsByTagName("p");//获取页面中的所有p元素
 8     for(var i=0;i < items.length;i++){    //循环
 9         items[i].onclick = function(){  //给每一个p添加onclick事件
10             //doing something...
11             alert("suc!");
12         }
13     }
14 }
15 </script>
16 </head>
17 <body>
18 <p>测试1</p>
19 <p>测试2</p>
20 </body>
21 </html>
时间: 2024-08-30 08:12:12

锋利的Jquery(p的onclick()事件)的相关文章

【锋利的jQuery】中全局事件ajaxStart、ajaxStop不执行

最近一直都在研究[锋利的jQuery],确实是一本好书,受益匪浅.但由于技术发展及版本更新等原因,里面还是有些坑需要踩的. 比如:第六章七节中提到的全局事件ajaxStart.ajaxStop照着案例敲结果并不会执行. 在查阅资料后,发现原来在jquery1.9+版本以后,ajax全局事件需绑定到document对象上才能触发. 下面是各版本不同写法: <html> <head> <meta charset="utf-8"> <style>

【学习笔记】锋利的jQuery(三)事件和动画

一.jQuery事件 1,加载事件 $(document).ready(function(){...}) //等同于$(function(){..}) $(window).load(function(){...}) //等同于window.onload = fn 2,基本事件绑定 bind(type,[.data],fn) //可绑定多个事件:bind("mouseover mouseout",fn) unbind(type,fn) //解除绑定 one(type,[.data],fn

JQuery 动态添加onclick事件

$('#div_id').click(function(){ show(1,2,this); });

给网页中的元素添加onclick事件(引自锋利的jQuery)

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content="editplus" /> <title>给网页中的元素添加onclick事件</p></title> <script src="jquery-1.7.1

锋利的jQuery第四章:jQuery中的事件和动画

第一部分 1, (1)$()是$(document)的简写,默认参数是document. $(function(){}是$(document).ready(function(){})的简写. 2, (1)事件绑定 bind(type [,data],fn); type是事件类型,有blur,focus,load,resize,scroll,unload,click,dbclick,mousedown,mouseup, mouseover,mousemove,mouseout,mouseenter

web前端之锋利的jQuery四:jQuery中的事件

web前端之锋利的jQuery四:jQuery中的事件 加载DOM: 执行时机: $(document).ready(function(){}); 详情解释 事件绑定: bind(event,data,function) 第一个参数是事件类型,类型包括:blur focus load resize unload click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter change select submit

Jquery 移除 html中绑定的onClick事件

HTML绑定示例: <button class="edit" onClick="showTurnEdit(this)">编辑</button> 需求: 在点击触发submit方法后,触发移除掉 .edit 中的onClick事件: 解决方案: 1.失败方案: $(".edit").removeAttr("onclick");//网上有些资料说jquery1.6+可以使用,但我试过不可以 2.成功方案:

JavaScript的 onclick 事件是如何调用jquery 方法的

看见个不错的问答,关于JavaScript的 onclick 事件是如何调用jquery 方法的,特此标注,链接如下:http://segmentfault.com/q/1010000000333507

巧妙使用Jquery 改变元素的 onclick 事件

需要点击图片将套组发布, 页面代码: Html代码   <img width="20px" src=" <s:property value="IMAGES_PATH" /><s:if test='%{releaseStatus == "YES"}'>pubed.png</s:if> <s:else>nopub.png</s:else>" onclick=&quo