lillll/li

創(chuàng)新互聯(lián)是專業(yè)的射洪網(wǎng)站建設(shè)公司,射洪接單;提供成都網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行射洪網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
li2222/li
li3333/li
script
$('li').on("click",?function?()?{
alert($(this).text()?+?'?li被點擊了');
});
/script
如下代碼可以實現(xiàn)點擊頁面獲得被點擊元素的id
$(document).click(function(e)?{?//?在頁面任意位置點擊而觸發(fā)此事件
$(e.target).attr("id");???????//?e.target表示被點擊的目標
})
示例代碼如下
創(chuàng)建Html元素
div?class="box"
span點擊頁面后,設(shè)置被點擊元素背景色并獲取其id:/spanbr
div?class="content"?id?=?"test"test
div?id?=?"test1"test1
div?id?=?"test2"test2
div?id?=?"test3"test3/div
/div
/div
/div
/div
設(shè)置css樣式
div.box{width:300px;padding:20px;margin:20px;border:4px?dashed?#ccc;}
div.box?span{color:#999;font-style:italic;}
div.content{width:250px;margin:10px?0;padding:20px;border:2px?solid?#ff6666;}
div.content?div{min-width:20px;min-height:20px;padding:30px;border:1px?solid?#446699;background:#ffffff;}
.bg{background:#ff99cc?!important;}
編寫jquery代碼
$(function(){
$(document).click(function?(e)?{
$(e.target).addClass('bg');?????//?設(shè)置背景色
alert($(e.target).attr('id'));??//?獲取id
})
})
觀察效果
初始狀態(tài)
點擊id為test2的div
為這個元素綁定單擊事件來偵聽br$("元素id或者類名").click(functionbr{br//處理邏輯bralert("俺被單擊了");br});
$(document).ready(function(){
$("li").children().click(function(){//當li下的子元素被點擊時觸發(fā)
alert('點擊了li下的'+$(this)[0].tagName);//獲取被點擊元素的標簽
});
});
可以試試我這個,因為jQuery選擇器的原因,所以一定要指定父級,否則會執(zhí)行多次。
div?id="parent"a?id="a1"?href="javascript:void(0);"點我/aa?id="a2"?href="javascript:void(0);"點我吧/ainput?id="b1"?type="button"?value="還是點我"/inputinput?id="b2"?type="button"?value="那點你吧"/input/div$("#parent *").click(function(e){if(e.target == $("#a1")[0]){alert("你點了鏈接一!");}else if(e.target == $("#a2")[0]){alert("你點了鏈接二!");}else if(e.target == $("#b1")[0]){alert("你點了按鈕一!");}else if(e.target == $("#b2")[0]){alert("你點了按鈕二!");}});
怎么使用jquery判斷一個元素是否含有一個指定的類(class)
在jQuery中可以使用2種方法來判斷一個元素是否包含一個確定的類(class)。兩種方法有著相同的功能。2種方法如下:
hasClass(‘classname’)
is(‘.classname’)
以下是一個div元素是否包含一個redColor的例子:
1.?使用is(‘.classname’)的方法
$('div').is('.redColor')
2.?使用hasClass(‘classname’)的方法(注意jquery的低版本可能是hasClass(‘.classname’))
$('div').hasClass('redColor')
以下是檢測一個元素是否含有一個redColor類的例子,含有時,則把其類變?yōu)閎lueColor。
html
head
styletype="text/css"
.redColor {
background:red;
}
.blueColor {
background:blue;
}
/style
scripttype="text/javascript"src="jquery-1.3.2.min.js"/script
/head
body
h1jQuery check if an element has a certain class/h1
divclass="redColor"This is a div tag with class name of "redColor"/div
p
buttonid="isTest"is('.redColor')/button
buttonid="hasClassTest"hasClass('.redColor')/button
buttonid="reset"reset/button
/p
scripttype="text/javascript"
$("#isTest").click(function () {
if($('div').is('.redColor')){
$('div').addClass('blueColor');
}
});
$("#hasClassTest").click(function () {
if($('div').hasClass('redColor')){
$('div').addClass('blueColor');
}
});
$("#reset").click(function () {
location.reload();
});
/script
/body
/html
網(wǎng)站題目:jquery元素被點擊,jquery中元素獲得焦點時觸發(fā)什么事件
標題來源:http://chinadenli.net/article0/dsgpioo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計、全網(wǎng)營銷推廣、建站公司、網(wǎng)站營銷、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)