JQuery的.load()内的divJQuery、load、div

2023-09-10 16:56:36 作者:空梦空得我心痛 *

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

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);
      });
});

也有