Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AJAX&;带锚的JQuery负载_Jquery_Ajax_Load_Anchor - Fatal编程技术网

AJAX&;带锚的JQuery负载

AJAX&;带锚的JQuery负载,jquery,ajax,load,anchor,Jquery,Ajax,Load,Anchor,下面是使用JQuery从external page.html加载内容的代码: 脚本: <script type="text/javascript"> $( function(){ var jContent = $( "#content" ); $( "nav ul li a").click(function( objEvent ){ var jLink = $( this ); ... $( 函数(){ var jContent=$(“#content”); $(“n

下面是使用JQuery从external page.html加载内容的代码:

脚本:

<script type="text/javascript">     
$(
function(){
var jContent = $( "#content" );

$( "nav ul li a").click(function( objEvent ){
var jLink = $( this ); ...

$(
函数(){
var jContent=$(“#content”);
$(“nav ul li a”)。单击(功能(对象){
var jLink=$(此)。。。
HTML


这里加载内容
脚本工作正常,但是…我希望将一些内容加载到一个特定的锚(page1.html#anchor)

我应该在脚本中添加什么?谢谢

///////////////////////

Hello最终通过以下代码解决:

<script> $(document).ready(function(){ $("a#dos").click(function(){
      $("#content").load("page2.html", function(responseTxt,statusTxt,xhr){ 
        if(statusTxt=="success")
        $(function() {
    $(document).scrollTop( $("#p2").offset().top );  });
        if(statusTxt=="error")
        alert("Error: "+xhr.status+": "+xhr.statusText);
      });
    }); ...
$(文档).ready(function(){$(“a#dos”)。单击(function()){
$(“#content”).load(“page2.html”,函数(responseText、statusTxt、xhr){
如果(statusTxt==“成功”)
$(函数(){
$(文档).scrollTop($(“#p2”).offset().top);});
如果(statusTxt==“错误”)
警报(“错误:+xhr.status+”:“+xhr.statusText”);
});
}); ...
HTML


…内容。。。
试试:


$(文档).ready(函数(){
$('.ajaxload')。单击(函数(){
$('#content_div').load($(this.attr('id')+'.html');
});
});
有关详细信息,请访问:


$(文档).ready(函数(){
$('.ajaxload')。单击(函数(){
$('#content_div').load($(this.attr('id')+'.html');
});
});

更多信息请访问根据您最后的评论,您需要将
事件
作为
内的参数传递,单击事件处理程序
,然后调用
预防默认值
,这样锚定将重定向您,因为您已经传递了
对象。

所以代码如下所示

$( "nav ul li a").click(function( objEvent ){
   objEvent.preventDefault();

根据您的上一条评论,您需要将
事件
作为
内的参数传递给
单击事件处理程序
,然后调用
预防默认值
,这样锚定将重定向您,您已经传递了
对象。

所以代码如下所示

$( "nav ul li a").click(function( objEvent ){
   objEvent.preventDefault();

你到底想做什么?如果我运行到一个不工作的锚的链接:page1.html#anchor你到底想做什么?如果我运行到一个不工作的锚的链接:page1.html#anchor你的代码工作很好,现在,就像我可以加载内容一样,也可以转到一个特定的片段作为锚示例:page1#anchor你的代码工作很好,现在,就像我可以做的一样加载内容并转到特定的片段作为锚定示例:page1#锚定
<script lang='javascript'>
        $(document).ready(function(){
            $('.ajaxload').click(function(){
                $('#content_div').load($(this).attr('id')+'.html');
            });
        });
</script>

<nav>
    <ul>
       <li><a class='ajaxload' href="javascript:void(0)" id='page1'>Page 1</a></li>
       <li><a class='ajaxload' href="javascript:void(0)" id='page2'>Page 2</a></li>
       <li><a class='ajaxload' href="javascript:void(0)" id='page3'>Page 3</a></li>
       <li><a href='#page4_content'>Page 4</a></li>
    </ul>
</nav>

<div id='content_div'>
    <!--Here your content gets loaded-->
</div>
<div id='page4_content'>
    <!--Page 4 content here-->
</div>
$( "nav ul li a").click(function( objEvent ){
   objEvent.preventDefault();