jquery - 使用 jQuery 获取 <div> 名称

标签 jquery css html attr

$("body").click(function() {
       var item = "<h1>You clicked on: #" + $(this).attr('id');
       $(".position").html(item);
     });

为什么这给我一个空白的回应?我希望它能够识别每个被点击的 HTML 对象的 ID。

最佳答案

试试这个

$("body").click(function(e) {
       var item = "<h1>You clicked on: #" + $(e.target).attr('id') + "</h1>";
       $(".position").html(item);
     });

关于jquery - 使用 jQuery 获取 <div> 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6804925/

相关文章:

PHP,openId - 无法写入mysql

jquery anchor 点击似乎不起作用

javascript - 使用 jquery 为每 7 个元素添加不同的类

css - 添加事件类而不是覆盖

html - 这是什么意思 : data-component-bound ="true"?

html - 如何读取和解析 R 中网页的内容

javascript - 使 textarea 文本加粗的按钮

javascript - 使用 id 或 class 获取元素类型

javascript - 使用javascript单击时在链接下划线

javascript - jquery mobile,google map body 高出 100%