[英]get href attribute on jQuery on click
我遇到了麻煩,嘗試了一切,但變量始終未定義= /
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#show").load("index.php");
});
$(document).on('click', ' a[href]', function (e) {
var a_href = $(this).find('a').attr('href');
alert ("Href is: "+a_href); //just for test
$("#show").load(a_href);
return false;
});
</script>
</head>
<body>
<a href="one.php">One</a>
<br>
<a href="two.php">two</a>
<div id="show"></div>
</body>
</html>
我實際上希望從#show div中獲取鏈接,並在不更改鏈接或重新加載頁面的情況下替換show div load,鏈接是在php中生成的,並且始終更改,這就是為什么我不能擁有靜態鏈接的原因。
您已經使用.on('click', ' a[href]',
選擇了鏈接.on('click', ' a[href]',
因此$(this).find('a')
尋找的鏈接是您單擊的鏈接的后代,但沒有不存在。
更改:
$(this).find('a').attr('href')
至
$(this).attr('href') // jQuery
要么
this.href // plain JavaScript as noted by epascarello
我會用這個:
.... your code
$('a').click(function (e) {
var a_href = $(this).attr('href');
.... your code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.