jQuery入门学习八:动态股票信息

本文的内容基本来自itcast上的视频教程,所用示例也基本是那上面的例子

 

这个例子的核心是tooltip功能的实现,就是当鼠标移到某个链接上,就会显示一些相应的信息,这个功能很常见,也很有用。

 

要实现这样的一个功能,一般需要这些信息:

  1. 股票名称,代码
  2. 股票当前价格,昨日收盘价,今日开盘价

 

因此,做这个demo要首先实现一个Stock类,里面包含有这些信息。为了模拟股票的变动,还需一个模拟程序,也可以不专门做一个类,而是放到处理客户请求的类中,做一个函数来实现。在本例中,正是使用了第二种方法,在GetStockInfo类里面随机地生成一些数字来模拟股市的变化。

 

这个demo的主要目的在于实现tooltip功能,tooltip的关键有这几点:

  1. 判断鼠标进入,并响应,并获取鼠标的坐标
  2. 判断鼠标离开,并响应
  3. 做一个div,用css控制其样式,用js来控制其显示

在这几点中,我开始觉得前面两个很难做到,因为以前写Windows程序,消息机制是挺复杂的一个东西,后来查了一些资料,才发现JavaScript本身就有处理这种事件的功能:event。几乎所有的鼠标,键盘操作都能产生相应的event,并且这些event带有丰富的事件源信息,如坐标位置,哪个按键等。在Windows下,我想起作用的大概还是用的消息机制,只不过浏览器对其进行了封装。因此,在浏览器级别上进行的开发,至少可以屏蔽不同操作系统消息机制带来的不同,是种跨越平台的良好选择。如果能在编程框架上有跨平台的能替代MFC的东西,那就更好了。

具体的步骤如下:

  • 创建一个html页面,包含几个标签,内容是要显示的股票和一个隐藏的div标签

 

  • 写控制页面表现以及处理和后台交互的js文件。里面有个setInterval()函数,这个函数很常用,也很有用。

 

jQuery至少在这几个方面给开发者带来了很大的方便,包括:

  1. 设置在页面载入时就执行的函数,使用的是$(document.ready(function(){})); 这个方法十分有用,使程序员可以在页面载入的时设置页面的表现,如改变div的css,以及显示或隐藏一些div等。
  2. 获取鼠标所在的标签,$(this)
  3. $.get(),前面已经说过了
  • 创建Stock类和GetStockInfo类,用来在后台处理用户的响应

Stock.java:

GetStockInfo.java:

 

 

虽然jQuery在这里的表现很精彩,但这个例子给我留下印象最深的却不是jQuery,而是JavaScript中的event。开始我以为event那么好用是jQuery的功劳,后来查了一些资料发现原来JavaScript自身的event就已经很强大了,以至于jQuery虽然也包装了一个Event对象,但和JavaScript中的Event几乎没有什么区别。


DOM Event:

 

Event Object

The Event object represents the state of an event, such as the element in which the event occurred, the state of the keyboard keys, the location of the mouse, and the state of the mouse buttons.

Events are normally used in combination with functions, and the function will not be executed before the event occurs!

IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet Standard).

Event Handlers

New to HTML 4.0 was the ability to let HTML events trigger actions in the browser, like starting a JavaScript when a user clicks on an HTML element. Below is a list of the attributes that can be inserted into HTML tags to define event actions.

AttributeThe event occurs when...IEFOW3C
onabortLoading of an image is interrupted419Yes
onblurAn element loses focus319Yes
onchangeThe content of a field changes319Yes
onclickMouse clicks an object319Yes
ondblclickMouse double-clicks an object419Yes
onerrorAn error occurs when loading a document or an image419Yes
onfocusAn element gets focus319Yes
onkeydownA keyboard key is pressed31NoYes
onkeypressA keyboard key is pressed or held down319Yes
onkeyupA keyboard key is released319Yes
onloadA page or an image is finished loading319Yes
onmousedownA mouse button is pressed419Yes
onmousemoveThe mouse is moved319Yes
onmouseoutThe mouse is moved off an element419Yes
onmouseoverThe mouse is moved over an element319Yes
onmouseupA mouse button is released419Yes
onresetThe reset button is clicked419Yes
onresizeA window or frame is resized419Yes
onselectText is selected319Yes
onsubmitThe submit button is clicked319Yes
onunloadThe user exits the page319Yes

Mouse / Keyboard Attributes

PropertyDescriptionIEFOW3C
altKeyReturns whether or not the "ALT" key was pressed when an event was triggered619Yes
buttonReturns which mouse button was clicked when an event was triggered619Yes
clientXReturns the horizontal coordinate of the mouse pointer when an event was triggered619Yes
clientYReturns the vertical coordinate of the mouse pointer when an event was triggered619Yes
ctrlKeyReturns whether or not the "CTRL" key was pressed when an event was triggered619Yes
metaKeyReturns whether or not the "meta" key was pressed when an event was triggered619Yes
relatedTargetReturns the element related to the element that triggered the eventNo19Yes
screenXReturns the horizontal coordinate of the mouse pointer when an event was triggered619Yes
screenYReturns the vertical coordinate of the mouse pointer when an event was triggered619Yes
shiftKeyReturns whether or not the "SHIFT" key was pressed when an event was triggered619Yes

Other Event Attributes

PropertyDescriptionIEFOW3C
bubblesReturns a Boolean value that indicates whether or not an event is a bubbling eventNo19Yes
cancelableReturns a Boolean value that indicates whether or not an event can have its default action preventedNo19Yes
currentTargetReturns the element whose event listeners triggered the eventNo19Yes
eventPhaseReturns which phase of the event flow is currently being evaluated   Yes
targetReturns the element that triggered the eventNo19Yes
timeStampReturns the time stamp, in milliseconds, from the epoch (system start or event trigger)No19Yes
typeReturns the name of the event619Yes

 

 

 

jQuery的Event

API/1.3/Events

This section details the event-related functions in the jQuery API. For more information about how jQuery handles and normalizes event properties and functions in a cross-browser manner, see thejQuery Event Object.

 

Page Load:
ready( fn )Returns: jQuery
Binds a function to be executed whenever the DOM is ready to be traversed and manipulated.
Event Handling:
bind( type, data, fn )Returns: jQuery
Binds a handler to one or more events (like click) for each matched element. Can also bind custom events.
one( type, data, fn )Returns: jQuery
Binds a handler to one or more events to be executed once for each matched element.
trigger( event, data )Returns: jQuery
Trigger an event on every matched element.
triggerHandler( event, data )Returns: Object
Triggers all bound event handlers on an element (for a specific event type) WITHOUT executing the browser's default actions, bubbling, or live events.
unbind( typefn )Returns: jQuery
This does the opposite of bind, it removes bound events from each of the matched elements.
Live Events:
live( type, fn )Returns: jQuery
Added in jQuery 1.3: Binds a handler to an event (like click) for all current - and future - matched element. Can also bind custom events.
die( typefn )Returns: jQuery
Added in jQuery 1.3: This does the opposite of live, it removes a bound live event.
Interaction Helpers:
hover( over, out )Returns: jQuery
Simulates hovering (moving the mouse on, and off, an object). This is a custom method which provides an 'in' to a frequent task.
toggle( fn, fn2, fn3,fn4,... )Returns: jQuery
Toggle among two or more function calls every other click.
Event Helpers:
blur( )Returns: jQuery
Triggers the blur event of each matched element.
blur( fn )Returns: jQuery
Bind a function to the blur event of each matched element.
change( )Returns: jQuery
Triggers the change event of each matched element.
change( fn )Returns: jQuery
Binds a function to the change event of each matched element.
click( )Returns: jQuery
Triggers the click event of each matched element.
click( fn )Returns: jQuery
Binds a function to the click event of each matched element.
dblclick( )Returns: jQuery
Triggers the dblclick event of each matched element.
dblclick( fn )Returns: jQuery
Binds a function to the dblclick event of each matched element.
error( )Returns: jQuery
Triggers the error event of each matched element.
error( fn )Returns: jQuery
Binds a function to the error event of each matched element.
focus( )Returns: jQuery
Triggers the focus event of each matched element.
focus( fn )Returns: jQuery
Binds a function to the focus event of each matched element.
keydown( )Returns: jQuery
Triggers the keydown event of each matched element.
keydown( fn )Returns: jQuery
Bind a function to the keydown event of each matched element.
keypress( )Returns: jQuery
Triggers the keypress event of each matched element.
keypress( fn )Returns: jQuery
Binds a function to the keypress event of each matched element.
keyup( )Returns: jQuery
Triggers the keyup event of each matched element.
keyup( fn )Returns: jQuery
Bind a function to the keyup event of each matched element.
load( fn )Returns: jQuery
Binds a function to the load event of each matched element.
mousedown( fn )Returns: jQuery
Binds a function to the mousedown event of each matched element.
mouseenter( fn )Returns: jQuery
Bind a function to the mouseenter event of each matched element.
mouseleave( fn )Returns: jQuery
Bind a function to the mouseleave event of each matched element.
mousemove( fn )Returns: jQuery
Bind a function to the mousemove event of each matched element.
mouseout( fn )Returns: jQuery
Bind a function to the mouseout event of each matched element.
mouseover( fn )Returns: jQuery
Bind a function to the mouseover event of each matched element.
mouseup( fn )Returns: jQuery
Bind a function to the mouseup event of each matched element.
resize( fn )Returns: jQuery
Bind a function to the resize event of each matched element.
scroll( fn )Returns: jQuery
Bind a function to the scroll event of each matched element.
select( )Returns: jQuery
Trigger the select event of each matched element.
select( fn )Returns: jQuery
Bind a function to the select event of each matched element.
submit( )Returns: jQuery
Trigger the submit event of each matched element.
submit( fn )Returns: jQuery
Bind a function to the submit event of each matched element.
unload( fn )Returns: jQuery
Binds a function to the unload event of each matched element.

 

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值