在jquery中获取自定义属性值

编程入门 行业动态 更新时间:2024-10-15 10:12:09
本文介绍了在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中获取自定义属性值

本文发布于:2023-11-15 22:15:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!