JQuery的.load()内的div [英] JQuery .load() inside div

查看:185
本文介绍了JQuery的.load()内的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我发疯!我已经搜索并尝试了一堆建议,看起来像他们应该工作,所以我必须做一些愚蠢的。

This has been driving me nuts! I've searched and tried a bunch of suggestions that seem like they should work, so I must be doing something foolish.

$(document).ready(function(){
  $('.trigger').click(function(){
  var link = $(this).attr("href");
    $('#target').load(link);
    return false;
  });
});

<div id='content'>
This is some stuff that I want left alone.
<a href="testload1.html" class="trigger">Click here</a>
<a href="testload2.html" class="trigger">Click here</a>
</div>
<div id="target">
</div>

我想在HTML文档testload1和testload2(其中只包含了一行字)加载到目标分区。

I'm trying to load the html document "testload1" and "testload2" (which just contain a line of text) into the "target" div.

相反,会发生什么情况是testload1页面只是加载作为一个正常的链接会。

Instead what happens is the "testload1" page just loads as a normal link would.

编辑:更多信息

我试着用以下无差异所建议的修改。这使我觉得有什么不对的文件的剩余部分,​​所以在这里它是:

I tried the suggested changes below with no difference. This leads me to think there is something wrong with the rest of the file, so here it is:

<!DOCTYPE html>
<head>
<title>test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script>
$(document).ready(function(){
  $('.trigger').click(function(){
  var link = $(this).attr("href");
    $('#target').load(link);
    return false;
  });
});
</script>
</head>
<body>
<div id='content'>
This is some stuff that I want left alone.
<a href="testload1.html" class="trigger">Click here</a>
<a href="testload2.html" class="trigger">Click here</a>
</div>
<div id="target">
</div>
</body>
</html>

是否有任何理由,这应该不是工作?

Is there any reason this shouldn't work?

推荐答案

也许你只需要prevent默认的动作,当你点击一个链接或查询网址。

Maybe you just need to prevent the default action when you click a link or check the url.

试试这个:

$(document).ready(function(){
    $('.trigger').click(function(e){
         e.preventDefault();
         var link = $(this).attr("href");
         $('#target').load(link);
      });
});

也有

这篇关于JQuery的.load()内的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆