链接内容已更改的 HTML 页面

问题描述 投票:0回答:1

这个问题的答案可能很简单,但由于我无法很好地表达它,我一直很难找到我正在寻找的内容的搜索结果。

这是我的问题:我的页面通过在“母”页面上显示纯 HTML 文件(即几乎没有 CSS 样式的 HTML 文件)来运行,该“母”页面具有所有 CSS 样式,以使一切看起来都很好。例如:假设在

https://mywebsite.com
我有以下代码:

<a href="#pictures" onclick="changecontent(1); return false;">click for pictures</a>
if (page == 1) $("#content").load("/pictures.html");

这个效果很好。但是,如果我想将人们专门链接到我页面上的图片部分...我怎样才能做到这一点而不链接实际的

pictures.html
?我已将
return false
更改为
return true
,点击此链接后,它更改了我的网页 URL:

https://mywebsite.com/#pictures

实际上将

https://mywebsite.com/#pictures
放入 URL 栏中只会将我带到默认的
https://mywebsite.com
。那么,我该如何链接内容已更改的页面呢?非常感谢!

javascript html jquery url hyperlink
1个回答
0
投票

以下是一些有关加载页面的各种方法的示例

<a href="/pictures.html" class="contentLink">click for pictures</a> 

$(() => {
  // clicking will load it into content - do NOT do this to have the link go to the page
  $('.contentLink').on('click',(e) => { 
    e.preventDefault(); // stop the link
    $("#content").load(e.target.closest('a').attr('href'));
  });
  // if page == 1 on page load, load the first of those links into content
  if (page == 1) {
    $("#content").load($('.contentLink').eq(0).attr('href'));
  })
});
© www.soinside.com 2019 - 2024. All rights reserved.