简体   繁体   English

使用直接链接触发onClick()事件

[英]Trigger onClick() event with direct links

I have a website with a navigation bar. 我有一个带有导航栏的网站。 When a link is clicked, it uses the onClick() handler to change the content of a page. 单击链接时,它使用onClick()处理函数更改页面的内容。

For example. 例如。 If the about us link is clicked, the page content will change to the about us content, and the URL will become xxxxx.com/#about . 如果单击关于我们链接,页面内容将更改为关于我们内容,URL将变为xxxxx.com/#about However, when linked directly to xxxxx.com/#about it opens the default index.php page. 但是,当直接链接到xxxxx.com/#about它将打开默认的index.php页面。

How do I directly link to the pages, as if it was onClick() ? 我如何直接链接到页面,就像它是onClick()

You can call the function used in onclick on page load. 您可以调用onclick页面加载中使用的函数。

window.location.hash 

will give you #about in your example, so you can load(or show) the relevant content. 将在您的示例中为您提供#about,因此您可以加载(或显示)相关内容。

You need get the #something on page load, and then if it is found, call your onclick function with it. 您需要获取#something on page load,然后,如果找到它,则用它调用onclick函数。

tag = /#[^\s]+/.exec(document.location.href);
tag = tag.substring(1);
// you have what you want

so you're loading in content with Ajax? 所以您要使用Ajax加载内容?

A quick and dirty way to do this would be to look at the url 一种快速而肮脏的方法是查看网址

document.location.href

You can get the action you're looking for like this 您可以像这样获得想要的动作

// get the url and split it at the "#" character into an array
var urlSplit = document.location.href.split("#");

// the 2nd item in the array will be the page
var page = urlSplit[1];

now you know the 'page', you can call whatever function that loads the page content 现在您知道了“页面”,您可以调用加载页面内容的任何函数

The #about is just pointing to a section within a page, not actually changing page. #about只是指向页面中的某个部分,实际上并未更改页面。 I'm guessing the onClick event refreshes the page with AJAX, and updates the URL shown to the user by changing document.location or similar. 我猜测onClick事件将使用AJAX刷新页面,并通过更改document.location或类似内容来更新显示给用户的URL。

Generally, I would say don't use AJAX for navigation. 通常,我会说不要使用AJAX进行导航。 If you're changing to a different page, let the browser do a proper update, so the back button will work as the user expects it to. 如果要切换到其他页面,请让浏览器进行适当的更新,以便后退按钮将按用户期望的方式工作。 Use AJAX when you're dynamically altering portions of a page. 动态更改页面部分时,请使用AJAX。

If you really want AJAX navigation, use window.location.hash, as suggested by strada. 如果您确实想要AJAX导航,请使用strada建议的window.location.hash。

But also be aware that IE can easily get into problems when using lots of Javascript language constructs. 但也要注意,当使用许多Javascript语言构造时,IE容易陷入问题。 Circular references between javascript and DOM objects, and closures can quickly cause memory leaks. javascript和DOM对象之间的循环引用以及闭包可能很快导致内存泄漏。 Other browsers also suffer but IE (including 9) seems to be worst affected. 其他浏览器也受到影响,但IE(包括9)似乎受到的影响最大。 If you refresh the page, that makes it really easy for the browser to throw out all this stuff. 如果刷新页面,那么浏览器真的很容易抛出所有这些东西。

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

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