jQuery:jQuery.ajax()和锚点的问题

时间:2022-10-08 07:26:56

Hi all I have a problem with anchors and dynamic content loaded thought $.ajax().

嗨,我有一个锚点和动态内容加载的问题$ .ajax()。

Let suppose this is my jQuery code:

假设这是我的jQuery代码:

$("a.false").live("click", function() {
    var data = $(this).attr("href");
    $("div#text").hide("fast");
    $("div#loader").show("fast");
    $.ajax({
        type: "GET",
        url: "callback.php",
        data: data,
        dataType: "html",
        cache: false,
        success: function(result){
            $("div#loader").hide("fast");
            $("input#data").val(data);
            $("div#text").html(result);
            $("div#text").show("fast");
        }
    });
});

I think everybody knows what means this code and I'll no explain it.

我想每个人都知道这段代码是什么意思,我不会解释它。

callback.php answer is:

callback.php答案是:

<div class="name">
    <div class="replic_id">
        <input type="hidden" id="replica_1" name="replica_1" value="1" />
        <a id="rp1">#1</a>
    </div>
    <div class="names">Name 1</div>
    <div class="replicas">Replica 1</div>
</div>
<div class="name">
    <div class="replic_id">
        <input type="hidden" id="replica_2" name="replica_2" value="2" />
        <a id="rp2">#2</a>
    </div>
    <div class="names">Name 2</div>
    <div class="replicas">Replica 2</div>
</div>

And now I javascript variable "data" value is = "movie=scarymovie#255" the page must start from anchor #255 but it starts from the begin. How I can fix it to start from anchor 255, no from begin?

现在我的javascript变量“data”值是=“movie = scarymovie#255”页面必须从#255开始,但它从开始开始。我如何解决它从锚255开始,从开始没有?

with regards ;]

带着敬意 ;]

Fixed: Correct a IDs

修正:更正ID

1 个解决方案

#1


1  

It is easier to send back the anchor as its own value and just use:

将锚点作为自己的值发送回来更容易,只需使用:

window.location.hash = '#255';

It is worth noting that even though it may work, 255 is not a valid value for an ID attribute in HTML: What are valid values for the id attribute in HTML?

值得注意的是,即使它可能有效,255也不是HTML中ID属性的有效值:HTML中id属性的有效值是什么?

Edit: fixed a typo in the variable name

编辑:修改变量名称中的拼写错误

#1


1  

It is easier to send back the anchor as its own value and just use:

将锚点作为自己的值发送回来更容易,只需使用:

window.location.hash = '#255';

It is worth noting that even though it may work, 255 is not a valid value for an ID attribute in HTML: What are valid values for the id attribute in HTML?

值得注意的是,即使它可能有效,255也不是HTML中ID属性的有效值:HTML中id属性的有效值是什么?

Edit: fixed a typo in the variable name

编辑:修改变量名称中的拼写错误