JS 事件(备用)



jQuery 事件 - trigger() 方法

触发 input 元素的 select 事件:
$("button").click(function(){
$("input").trigger("select");
});

代码:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input").select(function(){
$("input").after("文本被选中!");
});
$("button").click(function(){
$("input").trigger("select");
});
});
</script>
</head>
<body>
<input type="text" name="FirstName" value="Hello World" />
<br />
<button>激活 input 域的 select 事件</button>
</body>
</html>


jQuery 事件 - triggerHandler() 方法
触发 input 元素的 select 事件:
$("button").click(function(){
$("input").triggerHandler("select");
});

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input").select(function(){
$("input").after("发生 Input select 事件!");
});
$("button").click(function(){
$("input").triggerHandler("select");
});
});
</script>
</head>
<body>
<input type="text" name="FirstName" value="Hello World" />
<br />
<button>激活 input 域的 select 事件</button>
<p>请注意,与 trigger() 方法不同,triggerHandler() 方法不会引起所发生事件的默认行为(文本不会被选中)。</p>
</body>
</html>


window的onunload和onbeforeunload事件
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或者在<body>里指定。
区别在于onbeforeunload在onunload之前执行,它还可以阻止onunload的执行。

  Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读取新的页面时调用,此时还没开始读取;而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。

1、onbeforeunload事件:

  说明:目前三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
2、onunload事件
通过地址栏或收藏夹前往其他页面的时候

   ·点击返回,前进,刷新,主页其中一个的时候

   ·点击 一个前往其他页面的url连接的时候

   ·调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.

   ·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。

   ·重新赋予location.href的值的时候。

   ·通过input type=”submit”按钮提交一个具有指定action的表单的时候。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值