在jquery中获取自定义属性值 [英] get custom attribute value in jquery
本文介绍了在jquery中获取自定义属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个跨度,我有自定义属性。我希望通过点击< a>
来获取它的价值。这是我的HTML
i have two span's and where i have a custom attribute. I want to get the value of it by clicking <a>
.Here is my html
<span data="<?php echo $key; ?>"><?php echo $answer['vote']; ?></span>
<a href="#" class="tup" rel="<?php echo $answer['id']; ?>"><span class="glyphicon glyphicon-thumbs-up design-thumbs" id="thumbs-up-ico"></span></a>
Jquery
$(document).ready(function() {
$( ".tup" ).click(function(event) {
event.preventDefault();
var key = $('span').attr('data');
alert(key);
});
这里我在alert中未定义。我的错误是什么?谢谢
Here i get undefined in alert.Where is my error?Thanks
推荐答案
你是因为你没有选择想要的跨度,所以大多数人都未定义,使用.prev()来选择它
You are most likley getting undefined because you are not selecting the wanted span, use .prev() to select it
$( ".tup" ).click(function(event) {
event.preventDefault();
var key = $(this).prev().attr('data');
// ^^^^^^^^
alert(key);
});
这篇关于在jquery中获取自定义属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文