jQuery如何实现点击页面获得当前点击元素的id或其他信息

jQuery如何实现点击页面获得当前点击元素的id或其他信息,第1张

如下代码可以实现点击页面获得被点击元素的id

$(document)click(function(e) { // 在页面任意位置点击而触发此事件

  $(etarget)attr("id");       // etarget表示被点击的目标

})

示例代码如下

创建Html元素

<div class="box">

<span>点击页面后,设置被点击元素背景色并获取其id:</span><br>

<div class="content" id = "test">test

<div id = "test1">test1

<div id = "test2">test2

<div id = "test3">test3</div>

</div>

</div>

</div>

</div>

设置css样式

divbox{width:300px;padding:20px;margin:20px;border:4px dashed #ccc;}

divbox span{color:#999;font-style:italic;}

divcontent{width:250px;margin:10px 0;padding:20px;border:2px solid #ff6666;}

divcontent 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) {

$(etarget)addClass('bg');     // 设置背景色

alert($(etarget)attr('id'));  // 获取id

})

})

观察效果

初始状态

点击id为test2的div

按钮的onClick="abc(this);"通过这个this传到js里面 在js可以通过thisname thisid thisparent等等来获得相应的与此控件相关的值

按钮点击是调用 OnInit 事件的话

那 OnInit(object sender, EventArgs e)

(button)sender

应该就是 这个按钮了

((button)sender)id

以上就是关于jQuery如何实现点击页面获得当前点击元素的id或其他信息全部的内容,包括:jQuery如何实现点击页面获得当前点击元素的id或其他信息、JavaScript中如何获取按钮所在DIV的ID或NAME、求助,怎么获取当前页面点击Buttom按钮的ID等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9574561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存