我是新來的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'在哪裏? –