<!DOCTYPE html><html><head>? ? <meta http-equiv="Content-type" content="text/html; charset=utf-8" />? ? <title></title>? ? <style>? ? .test2 {? ? ? ? background: #bbffaa;? ? }? ? .test3 {? ? ? ? background: yellow;? ? }? ? .test2,.test3{? ? ? ? border: 1px solid red;? ? }? ? </style>? ? <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script></head><body>? ? <h2>.click()方法</h2>? ? <h4>測試一</h4>? ? <button>元素綁定事件,彈出回調(diào)中的this</button>? ? <script type="text/javascript">? ? ? ? //this指向button元素? ? ? ? ?$("button").eq(1).click(function() {? ? ? ? ? ? alert(this)? ? ? ? })? ? </script>? ? <h4>測試二</h4>? ? <div class="test2">? ? ? ? <p>$('button:first').click(function(e) {alert(this)})</p>? ? </div>? ? <button>指定觸發(fā)事件</button>? ? <script type="text/javascript">? ? ? ? $('p').click(function(e) {? ? ? ? ? ? alert(e.target.textContent)? ? ? ? })? ? ? ? //this指向button元素? ? ? ? $("button").eq(0).click(function() {? ? ? ? ? ? $('p').click() //指定觸發(fā)綁定的事件? ? ? ? })? ? </script>? ? <h4>測試三</h4>? ? <div class="test3">? ? ? ? <p>$('.right').click(1111, set)</p>? ? </div>? ? <button>不同函數(shù)傳遞數(shù)據(jù)</button>? ? <script type="text/javascript">? ? ? ? //不同函數(shù)傳遞數(shù)據(jù)? ? ? ? function data(e) {? ? ? ? ? ? alert(e.data) //1111? ? ? ? }? ? ? ? function a() {? ? ? ? ? ? $("button").eq(2).click(1111, data)? ? ? ? }? ? ? ? a()? ? </script></body></html>
3 回答
已采納

OlderSkee
TA貢獻123條經(jīng)驗 獲得超103個贊
$("button").eq(1)這個元素在這句js的后面
所以獲取不到
把這語話移到
<button>指定觸發(fā)事件</button>
的后面 就可以了 。。

Caballarii
TA貢獻1123條經(jīng)驗 獲得超629個贊
因為eq(1)是選第二個元素,但這句話執(zhí)行的時候第二個button還沒有出來,所以沒綁上,建議以后寫jquery都放到$(function() {})或者$(document).ready(function(){ })里,保證html完全渲染以后才執(zhí)行js

慕虎1618318
TA貢獻18條經(jīng)驗 獲得超6個贊
?$("button").eq(1)如果你是先給第一個按鈕添加事件,應(yīng)該是??$("button").eq(0),索引是從0開始的
- 3 回答
- 0 關(guān)注
- 2720 瀏覽
添加回答
舉報
0/150
提交
取消