I've got troubles, tried everything but variable always undefined =/
我有麻烦,尝试了一切但变量总是未定义= /
<!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>
I actually want links to be taken from #show div and replace show div load after it without changing the link or reloading the page, the links are generated in php and always change this is why i cant have static links.
我实际上想要从#show div获取链接并在它之后替换show div load而不更改链接或重新加载页面,链接在php中生成并且总是改变这就是为什么我不能拥有静态链接。
2 个解决方案
#1
2
You already select the link with .on('click', ' a[href]',
so $(this).find('a')
is looking for a link that's a descendant of the link that you clicked on, which doesn't exist.
你已经选择了.on的链接('click','a [href]',所以$(this).find('a')正在寻找一个链接,它是你点击的链接的后代,不存在。
Change:
$(this).find('a').attr('href')
to
$(this).attr('href') // jQuery
or
this.href // plain JavaScript as noted by epascarello
#2
0
I would use this:
我会用这个:
.... your code
$('a').click(function (e) {
var a_href = $(this).attr('href');
.... your code
#1
2
You already select the link with .on('click', ' a[href]',
so $(this).find('a')
is looking for a link that's a descendant of the link that you clicked on, which doesn't exist.
你已经选择了.on的链接('click','a [href]',所以$(this).find('a')正在寻找一个链接,它是你点击的链接的后代,不存在。
Change:
$(this).find('a').attr('href')
to
$(this).attr('href') // jQuery
or
this.href // plain JavaScript as noted by epascarello
#2
0
I would use this:
我会用这个:
.... your code
$('a').click(function (e) {
var a_href = $(this).attr('href');
.... your code