為什么用focus傳遞參數(shù)獲取失敗,用click獲取成功
代碼如下
<!DOCTYPE html>
<html>
<head>
??? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
??? <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
???????????????? <input type="text">
??? <br>
???????????????? <button>focus傳參數(shù)</button>
??? <br>
???????????????? <button>click傳參數(shù)</button>
<script type="text/javascript">
???? $("input").on("focus",function(event,title) {
??????? alert(title)? //為什么參數(shù)傳遞失敗
??? });
??? $("button:first").click(function(){
???????? $("input").trigger("focus","focus傳參數(shù)");
??? })
?? ?
?? ?
?? ?
???? $("input").on("click",function(event,title) {
??????? alert(title)
??? });
??? $("button:last").click(function(){
?????? $("input").trigger("click","click傳參數(shù)");
??? })
?? ?
?? ?
</script>
</body>
</html>
2017-04-07
因?yàn)閠rigger會(huì)觸發(fā)瀏覽器的默認(rèn)行為 即input的聚焦行為,此時(shí)是不能將input設(shè)置title值的;而triggerHandler不會(huì)觸發(fā)瀏覽器的默認(rèn)行為 即input的聚焦行為,此時(shí)是可以將input設(shè)置title值的。
就這樣。
2018-10-09
trigger除了能夠觸發(fā)瀏覽器事件(原生事件),同時(shí)還支持自定義事件,并且自定義事件還支持傳遞參數(shù),
自定義事件支持傳遞參數(shù),但原生事件(瀏覽器事件)并不支持相同的形式傳遞參數(shù),
$("input").trigger("focus","focus傳參數(shù)");//在這里觸發(fā)的是原生事件,不支持傳遞參數(shù),寫(xiě)了沒(méi)用,所以title的值為空
$("input")..triggerHandler("focus","focus傳參數(shù)");//觸發(fā)的是自定義事件,支持傳遞參數(shù),所以title的值為"focus傳參數(shù)"
2017-08-30
我今天來(lái)復(fù)習(xí)jquery ?然后又有點(diǎn)搞不懂這個(gè)問(wèn)題了,,,,結(jié)果來(lái)這里看到了以前自己寫(xiě)的答案。 贈(zèng)人玫瑰,手留余香。
2017-04-02
因?yàn)槟阌缅e(cuò)函數(shù)了 ?這個(gè)函數(shù)不能傳遞參數(shù) 請(qǐng)用 ?
$("input").focus(11111,function(e) {
? ? alert(e.data)
});