简体   繁体   English

如何使用ID名称作为URL使用jquery加载页面

[英]how to load page with jquery using the id name as url

how can i tell jquery to use the <a id=""> or <a href=""> as a URL to load on click? 如何告诉jquery使用<a id=""><a href="">作为URL来加载点击? For example URL page1.php. 例如URL page1.php。 either by getting the href, or by getting the id and then appending .php 通过获取href或获取ID,然后附加.php

html html

<a id="page1" class="load" href="page1.php">1</a>
<a id="page2" class="load" href="page2.php">2</a>
<a id="page3" class="load" href="page3.php">3</a>

jquery jQuery的

<script>
    $(document).ready(function () {  
        $(".load").click(loadDynamic);  
    });  
    function loadDynamic() {  
        $("#load_in")  
            .load("",function (content) {  
                $(this).hide().fadeIn("slow");  
                return false;  
        }); 
    }  
</script>

Use this.href to get the href . 使用this.href获取href

$(document).ready(function() {
    $(".load").click(loadDynamic);
});

function loadDynamic() {
    $("#load_in").load(this.href, function(content) {
        $(this).hide().fadeIn("slow");
    });
        return false;
}​

Of course you can use this.id + ".php" as well. 当然,您也可以使用this.id + ".php"

Note that you wrote return false inside the load callback thus it won't prevent the default anchor click! 请注意 ,您在load回调中编写了return false ,因此不会阻止默认的锚点单击! You should move it to the outer scope like in the answer. 您应该像答案中那样将其移至外部范围。


For those of you complaining about .click(fn) against on('click', fn) 对于那些抱怨on('click', fn) .click(fn) on('click', fn)

Those are the same function , .click(fn) is an alias for on('click', fn) : 这些是相同的函数.click(fn)on('click', fn)的别名:

In the first two variations, this method is a shortcut for .bind("click", handler), as well as for .on("click", handler) as of jQuery 1.7. 在前两个变体中,此方法是jQuery 1.7中.bind(“ click”,handler)以及.on(“ click”,handler)的快捷方式。

jQuery docs jQuery文档

You can use this.href : 您可以使用this.href

$(".load").click(function() {
    $("#load_in").load(this.href, function() {
        $(this).hide().fadeIn("slow");
    });
    return false;
});

To get ID use simple this.id . 要获取ID,请使用简单的this.id

Replace 更换

.load("",function (content) {

With

.load(this.href, function (content) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM