我是新来的jQuery和试图使一个http请求,让我不必离开当前页面,但是jQuery的道具()返回undefined
var getURL = $("#callerId").attr("href");
似乎并不奏效 执行console.log(使用getURL)显示 “未定义”
有源代码(都记录下来,以CALLERID应该是正确的)
我是新来的jQuery和试图使一个http请求,让我不必离开当前页面,但是jQuery的道具()返回undefined
var getURL = $("#callerId").attr("href");
似乎并不奏效 执行console.log(使用getURL)显示 “未定义”
有源代码(都记录下来,以CALLERID应该是正确的)
您需要使用
var getURL = $(this).attr("href");
代替
var getURL = $("#callerId").attr("href");
在第一种情况下$(this)
指向刚点击,在第二锚的jQuery对象 - 它指出了一些元素甚至不是你的榜样覆盖
我觉得你的意思是这样做的:
<script type = "text/javascript">
$(document).ready(function() {
$('ul.hrefURL a').click(function(e) {
e.preventDefault();
var getURL = this.href;
$.ajax({
type : "GET",
url: getURL,
success: function(response){
alert($('#response'));
}
});
});
});
</script>
需要了解的就是你以前的代码:
this
将被自动设置为引起该事件,所以你不必获得target.id
的对象。this.href
获取href的值。没有必要为此使用jQuery。this
的值设置为jQuery对象,您可以使用$(this)
或$(e.target.id)
来完成。当它是一个CSS选择器时,您可以使用带有jQuery的字符串。如果已经有对象引用,则直接使用变量值。
'#callerId'在哪里? –