我需要从href按钮获取值。在这里,我刚刚开发了一个显示所有href值的代码。其实我需要从点击的位置获得价值。Jquery获取href值
这里是我的代码
$(document).ready(function() {
$("a.me").click(function(){
var number = $('a').text();
alert(number);
});
});
我需要从href按钮获取值。在这里,我刚刚开发了一个显示所有href值的代码。其实我需要从点击的位置获得价值。Jquery获取href值
这里是我的代码
$(document).ready(function() {
$("a.me").click(function(){
var number = $('a').text();
alert(number);
});
});
在jQuery中可以使用$(this)来引用被点击的元素(或以其他方式使用)
像这样:
$("a.me").click(function(){
var number = $(this).text();
alert(number);
});
你应该做
$(document).ready(function() {
$("a.me").click(function(){
//It's not clear if you want the href attribute or the text() of the <a>
//this gives you the href. if you need the text $(this).text();
var number = this.href;
alert(number);
});
});
$("a.me").click(function(){
var number = $(this).attr('href');
alert(number);
});
这个答案的效果最好。即#anchor而不是website.com/page.html#anchor或点击文本的值。 – spitfire 2014-11-18 19:16:00
var number = $(this).attr('href');
不需要使用$(this).attr('href')',你正在做两个额外的jQuery调用 – 2012-03-12 12:48:51
$(document).ready(function() {
$("a.me").click(function(){
var target=$(this).attr("href");
alert(target);
});
});
其中一个答案上面会工作,或者如果你只需要锚,你可以使用
...
$("a.me").click(function(e){
var number = $(e.target).text();
alert(number);
});
试$(本),而不是$(一) – 2012-03-12 12:46:16