jQuery:获取标题和href值作为变量 [英] jQuery: Get title and href values as variables

查看:79
本文介绍了jQuery:获取标题和href值作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标题和href值的链接列表.我希望能够分别获得这些值,但是我总是获得第一个链接的值.这是为什么? 在这里查看我的小提琴.如您所见-单击任何链接时,总是从第一个链接获取值.我想设置这些变量是不够的:

I have a list of links with a title and a href value. I would like to be able to get these values seperately, but I always get the first link's values. Why is that? See my fiddle here. As you can see - when clicking a any of the link, you always get the values from the first link. I guess setting these variables isn't sufficient:

var title = $('.mg_phones').attr('title');
var url = $('.mg_phones').attr('href');

有什么想法吗?

推荐答案

您必须引用被单击的元素:

You have to refer to the clicked element:

var title = $(this).attr('title');
var url = $(this).attr('href');

这篇关于jQuery:获取标题和href值作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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