jQuery-使用"click"事件时如何获取元素的属性 [英] jQuery - How to get attribute of an element when using the 'click' event

查看:68
本文介绍了jQuery-使用"click"事件时如何获取元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery来单击'a'元素.我列出了这些链接,其中每个链接都有一个类,由他来捕获点击.

I'm using jQuery to get a click on an 'a' element. I have a list of those links where each one has a class which by him I'm capturing the click.

我想知道单击了哪些链接(我对每个链接都有一个属性"setid"),并且还获得了单击鼠标的位置.

I would like to know which of the links has been clicked (I have an attribute 'setid' for each link) and also get the point where the mouse has been clicked.

这怎么实现?

代码示例:

<a href="#" class="newItem" setid="28">click me</a>

$('.newItem').click(function (e) {
    alert(e.attr('setid'));
});

好的,要获得该职位,我将使用e.pageX

OK, so to get the position I would use e.pageX

推荐答案

要使用jQuery方法,必须将 this 与jQuery调用包装在一起.

To use jQuery methods you have to wrap this with a call to jQuery.

$('.newItem').click(function () {
    alert($(this).attr('setid'));
});

这篇关于jQuery-使用"click"事件时如何获取元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆