0

我试图在单击它后获取 href 值,因此我可以使用<?php include 'list'; ?>来加载我之前单击的 php 页面的内容。我正在尝试这样的事情:

$(document).ready(function() {
$('list').click(function)
var page=$(href);

<ul id="list">
<li><a href="mypage.php">Mypage</a> </li>
<li><a href="mypage2.php">Mypage2</a> </li>

所以我可以使用我之前点击的链接的内容在我的页面中加载一个段落?

4

2 回答 2

2

您可以获取 href 值,然后使用适当的选择器和任何适当的 jQuery ajax 函数(如ajax()load() )加载链接页面的内容。

$(document).ready(function() {
    $('#list > li a').click(function(event) {
        var page = this.attr("href");

        // Here should go your ajax code or what-have-you to load the content.
        // If you use an ajax function, the url will be equal to the var page.
        event.preventDefault();

        $.ajax({
            type: "POST", // or "GET" may be better for your needs
            url: page,
            data: "",
            success: function(content) {
                console.log("ajax success");
                $("#divID").html(content);
            },
            error: function() {
                alert("ajax error");
            },
            complete: function() {
                console.log("ajax complete");
            }
        });
    });
});

<ul id="list">
  <li><a href="mypage.php">Mypage</a> </li>
  <li><a href="mypage2.php">Mypage2</a> </li>
</ul>

代码缺少告诉 jQuery 查找 id 'list' 的哈希值。'#list > li a' 告诉 jQuery 将代码块应用到 #list 的所有 'li a' 子级。

于 2012-09-08T00:11:59.070 回答
1
//thanks a lot for your response, i real apreciate it
//i finally used this and works fine
$(function() {
$("#team-tabs a").click(function() {
    var page= this.hash.substr(1);
    $.get(page+".php",function(gotHtml) {
        $("#cv-content").html(gotHtml);
    });
    return false;
});
});

//for this:
<ul id="team-tabs">
    <li><a href="#team/cv1">name 1</a></li>
    <li><a href="#team/cv2">name 2</a></li>
于 2012-09-08T11:08:33.020 回答