当内联链接“#"被点击或输入到url中,就像链接到reddit评论时一样

How can I run a jQuery command when an inline link "#" is clicked or entered into the url like when linked to reddit comments

本文关键字:链接 reddit 评论 一样 url 输入 quot      更新时间:2023-09-26

我想在单击内联链接时运行脚本,或者在URL中输入类似http://site.com/index.php#comment.类似于reddit上链接到评论时发生的情况。

我该怎么做?

我会给你一个主意。。假设您要处理所有的a标签

html

<a href="http://site.com/index.php#comment">Link<a>

javascript

$('a').click(function(event){
event.preventDefault();
var url = $(this).attr('href');
var lastPart = url.split('#')[1];
if(lastPart=== 'comment') {
//do something
} else if(lastPart === 'post') {
//do something
}
});