Javascript Jquery获取href值
我需要从href按钮获取值。在这里,我刚刚开发了一个显示所有href值的代码。实际上,我需要从单击的位置获取值 这是我的密码Javascript Jquery获取href值,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我需要从href按钮获取值。在这里,我刚刚开发了一个显示所有href值的代码。实际上,我需要从单击的位置获取值 这是我的密码 $(document).ready(function() { $("a.me").click(function(){ var number = $('a').text(); alert(number); }); }); 你应该这样做 $(document).ready(function() { $("a.me").click(funct
$(document).ready(function() {
$("a.me").click(function(){
var number = $('a').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);
});
});
$(文档).ready(函数(){
$(“a.me”)。单击(函数(){
//还不清楚您是想要的href属性还是
//这将为您提供href.if您需要文本$(this.text();
var number=this.href;
警报(编号);
});
});
在jQuery中,可以使用$(this)引用单击(或以其他方式使用)的元素
像这样:
$("a.me").click(function(){
var number = $(this).text();
alert(number);
});
以上答案中的一个会起作用,或者您可以使用
...
$("a.me").click(function(e){
var number = $(e.target).text();
alert(number);
});
无需使用
$(this).attr('href')
,您将进行两个额外的jQuery调用如果您只需要锚,这个答案最有效。i、 e.#锚定而不是website.com/page.html#锚定或点击文本的值。
$("a.me").click(function(){
var number = $(this).text();
alert(number);
});
$(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);
});