Home Web Front-end HTML Tutorial 浅析html input 等值改变添加监听事件_HTML/Xhtml_网页制作

浅析html input 等值改变添加监听事件_HTML/Xhtml_网页制作

Jun 01, 2016 pm 02:22 PM
html input Listen for events

要达到的效果
很多情况下我们都会即时监听输入框值的变化,以便作出即时动作去引导浏览者增强网站的用户体验感。比如即时显示输入框已经被输入的字节数,或者即时读取输入的值来进行搜索引导,也就是google的关联搜索效果等。
只要我们能捕获即时事件就能做到很多事情。
需要了解的知识
首先,我们需要了解onchangeonpropertychange的不同:

IE下,当一个HTML元素的属性改变的时候,都能通过 onpropertychange来即时捕获。
onchange在属性值改变时还必须使得当前元素失去焦点(onblur)才可以激活该事件。

了解这一点后我们发现onpropertychange的效果就是我们想要的,可是很遗憾,它只在IE下有效果。我们能不能找到另外一个时间来代替onpropertychange呢?

经过翻阅资料得知,在其他浏览器下可以使用oninput事件来达到同样的效果,真是太好了,我们只需要把IE浏览器区分出来就可以。

oninput的使用

下面我们先了解一下oninput如何使用。
 如果您是将注册时间直接写在页面里面,那么如下写法就可以实现:


但是,将oninput写在JS代码中分离出来时与普通事件注册的方法有些不同,必须使用addEventListener来注册。

attachEvent和addEventListener 的不同

说到这里我们再来了解一下 attachEvent和addEventListener 的使用方法:

attachEvent方法,为某一事件附加其它的处理事件。(不支持Mozilla系列)
addEventListener方法 用于 Mozilla系列

举例:

XML/HTML Code复制内容到剪贴板
  1. document.getElementByIdx_x_x("btn").onclick = method1;   
  2.   
  3. document.getElementByIdx_x_x("btn").onclick = method2;   
  4.   
  5. document.getElementByIdx_x_x("btn").onclick = method3;   

如果这样写,那么将会只有medhot3被执行

写成这样:

XML/HTML Code复制内容到剪贴板
  1. var btn1Obj = document.getElementByIdx_x_x("btn1");   
  2.   
  3. btn1Obj.attachEvent("onclick",method1);   
  4.   
  5. btn1Obj.attachEvent("onclick",method2);   
  6.   
  7. btn1Obj.attachEvent("onclick",method3);    

执行顺序为method3->method2->method1

如果是Mozilla系列,并不支持该方法,需要用到addEventListener

XML/HTML Code复制内容到剪贴板
  1. var btn1Obj = document.getElementByIdx_x_x("btn1");   
  2.   
  3. btn1Obj.addEventListener("click",method1,false);   
  4.   
  5. btn1Obj.addEventListener("click",method2,false);   
  6.   
  7. btn1Obj.addEventListener("click",method3,false);   
  8.   
  9. 执行顺序为method1->method2->method3    

了解了如何使用addEventListener来注册oninput事件后我们再回到要解决的问题[划分浏览器]。

判断IE浏览器

如何将IE区分出来呢?
这似乎是一个老生常谈的问题,网络中有很多找那个方法,归类为两类:
其一,是判断浏览器的功能属性。
其二,就是判断传统的 user-agent 字符串,这可能是最古老也是最流行的检测方式。
在这里就不做深入了解了,我们这里用一种比较简单的方法来判断

XML/HTML Code复制内容到剪贴板
  1. if("\v"=="v") {   
  2.   
  3.   alert("IE");   
  4.   
  5. }else{   
  6.   
  7.   alert("NO");   
  8.   
  9. }   
  10.   

到目前为止我们遇到的问题就已经解决了,开始写代码来测试我们的思路是否能够实现。

以上这篇浅析html input 等值改变添加监听事件就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

原文地址:http://www.web600.net/html/editor/JavaScript/201001131529.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles