简体   繁体   English

如何使AJAX加载div顶部的页面

[英]How to get AJAX to load page at top of div

My site contains a div where different pages are loaded using ajax. 我的网站包含一个div,其中使用ajax加载了不同的页面。 On a button click, a new page can be loaded and the pages always appear in that div. 单击按钮后,可以加载新页面,并且页面始终显示在该div中。

The problem is if I go to a new page and scroll down without scrolling back to the top of that page, new pages will load at the same position below the top of the page, not at the top of the page. 问题是,如果我转到新页面并向下滚动而不滚动回到该页面的顶部,则新页面将加载到页面顶部下方的同一位置,而不是页面顶部。 I want to force all pages to load at the top. 我想强制所有页面加载到顶部。

Here are the relevant page elements: 以下是相关的页面元素:

The div (where the page content goes): div(页面内容所在的位置):

<div class="main_text">
<div id="C2"><span style="color:black">MTX</span></div>
</div>

The button: 按钮:

<div class="C1"><br><button class="button_01" id="btn07" onclick="HideDropdown(); ShowAjax(7);">Page 7</button></div>

The AJAX code: AJAX代码:

<script>
function ShowPage(type) {
    var filename = "page_" + type + ".htm"
    $( "#C2" ).hide().load( filename ).fadeIn(500);
}
</script>

The most important line is: 最重要的一行是:

$( "#C2" ).hide().load( filename ).fadeIn(500);

At the top of each page I have: 在每个页面的顶部,我有:

<div id="bkmk00"></div>

I want to open each page at the top of the page (at "bkmk00). I saw a post that said I could add an id name and it would load the page at that ID: 我想在页面顶部(“ bkmk00”)打开每个页面。我看到一个帖子,说我可以添加一个id名称,它将以该ID加载页面:

$( "#C2" ).hide().load( filename "bkmk00" ).fadeIn(500);

but with that, it doesn't load anything. 但这不会加载任何内容。 Instead, the Firefox 67 dev console says "SyntaxError: missing ) after argument list" for this ajax function call, and it points to character 37 which is right after filename. 取而代之的是,Firefox 67开发人员控制台针对此ajax函数调用说“参数列表后缺少SyntaxError:缺少),它指向紧随文件名之后的字符37。 That looks like I can't add the second parameter "bkmk00." 看来我无法添加第二个参数“ bkmk00”。

My question is, how can I get the page to load at "bkmk00" at the top of the page to be loaded? 我的问题是,如何才能将页面加载到要加载页面顶部的“ bkmk00”?

<script>
function ShowPage(type) {
    var filename = "page_" + type + ".htm"
    $( "#C2" ).hide().load( filename ).fadeIn(500);
}
</script>

You are close but not quite, load function has 2 parameters, input file and a function callback: 您很接近但不太完全,load函数有两个参数,输入文件和一个函数回调:

var filename = "page_" + type + ".htm"
$( "#C2" ).hide()
$( "#bkmk00" ).load(filename, function(){
    $( "#bkmk00" ).fadeIn(500);
});

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

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