0

我被困在试图检索类别id从 HTML5 中的超链接中检索类别。首先,我使用 jQuery 解析了 HTML5 中的 JSON 链接。

我的代码: HTML 代码: jquery 代码:

<script src="jquery-1.8.3.min.js"></script>
<script>
    $.getJSON('http://'    '/storejson.php', function(data) {
        var output="<ul>";
        for (var i in data.items) {
            output += "<li>" + data.items[i].categories_name + " " "</li>";
        }
        output += "</ul>";
        document.getElementById("placeholder").innerHTML=output;
    });
</script>

HTML 代码:

<a href="http://'     '/storesubcatjson.php?cat_id=&cat_id"><div id="placeholder" align="justify"></a>

我已经给出了一个超链接到上面的占位符。通过解析 JSON 显示类别,但我想通过超链接显示子类别。所以请告诉我我怎样才能做到这一点?

4

1 回答 1

0

您可以做的是从 a 标签中获取 href,然后在单击时将链接传递给您的 getjson,或者最适合您的方式,这会将请求发送到 php 文件,但请记住,您的 php 文件也必须设置从 href 中获取 url 字符串中传递的参数。

         <script>
    var link;

 $("a").click(function(){link = $(this).attr("href")}); // get this inside a click event on the 'a' tag
     alert(link);// to see the url being passed
    $.getJSON(link, function(data) {
             var output="<ul>";
             for (var i in data.items) {
             output += "<li>" + data.items[i].categories_name + " " "</li>";
       }
       output += "</ul>";
    document.getElementById("placeholder").innerHTML=output;
});
 </script>

 <a href="http://storesubcatjson.php?cat_id=&cat_id">CLICK</a> // If you click here it should send the href data
于 2013-01-11T13:27:58.637 回答