如何使用onclick事件在同一窗口和选项卡中打开链接?

时间:2021-12-10 01:27:18

I have a page that has multiple divs. I want to get some information from my database to display in some of those divs and I also want it to be displayed as I click on a link to the home div.

我有一个有多个div的页面。我想从我的数据库中获取一些信息来显示在一些div中,我还想在点击home div的链接时显示它。

I also need the page to be refreshed or reopened in the same window (not in a new page or tab). Last of all, I need the page to be in the home div.

我还需要在同一个窗口中刷新或重新打开页面(不是在新的页面或选项卡中)。最后,我需要页面在home div中。

I tried the code below and it didn't work:

我尝试了下面的代码,但没有成功:

<a href="#home" onclick="window.open('index.jsp#home')" >home</a>

<a href="#home" onclick="location.reload()">home</a>

<a href="#home" onclick="location.href='index.jsp'">home</a>

4 个解决方案

#1


13  

I used this and it worked

我用了这个,它起作用了

<a href="#" class="home_btn" onclick="location.reload();location.href='index.jsp'">منوی اصلی</a> 

#2


10  

change your :

改变你的:

onclick="window.open('index.jsp#home')" >home</a>

to

onclick="parent.location='index.jsp#home'">home</a>

no need to reload.

不需要重新加载。

#3


0  

like this?

像这样的吗?

<input id="but1" type="button" value="click"></div>

function loadIndex() {
    window.location.href = "http://jsfiddle.net/Xotic750/u5nmt/";
}

document.getElementById("but1").addEventListener("click", loadIndex, false);

on jsfiddle

在jsfiddle

remember jsfiddle is in frames

记住,jsfiddle是帧

#4


0  

The problem with changing location.href to the current URL with a hash value is that the page won't reload but jump to the given ID.

改变位置的问题。href到当前具有散列值的URL,即页面不会重新加载,而是跳转到给定的ID。

If you really want to jump to the home div then you can just jump with location.href='index.jsp' and edit your index file to set location.href = '#home' on load.

如果你真的想要跳转到home div你可以直接跳转到location。href='index。并编辑索引文件以设置位置。href = '#home' on load。

If you want to be able to pass information across to the newly loaded page (to provide a specific id) you could use the query string instead, e.g. to jump page use location.href = 'index.jsp?loaddiv=foo

如果您希望能够将信息传递到新加载的页面(以提供特定的id),您可以使用查询字符串,例如,跳转页面使用位置。href = ' index . jsp ? loaddiv = foo

Then once the page loads read the query string and use the value to jump to the requested div, e.g. location.search = '#foo'

然后页面加载之后,读取查询字符串并使用该值跳转到所请求的div,例如location。搜索= ' # foo '

For details on extracting values from the query string see this question.

有关从查询字符串提取值的详细信息,请参见这个问题。

#1


13  

I used this and it worked

我用了这个,它起作用了

<a href="#" class="home_btn" onclick="location.reload();location.href='index.jsp'">منوی اصلی</a> 

#2


10  

change your :

改变你的:

onclick="window.open('index.jsp#home')" >home</a>

to

onclick="parent.location='index.jsp#home'">home</a>

no need to reload.

不需要重新加载。

#3


0  

like this?

像这样的吗?

<input id="but1" type="button" value="click"></div>

function loadIndex() {
    window.location.href = "http://jsfiddle.net/Xotic750/u5nmt/";
}

document.getElementById("but1").addEventListener("click", loadIndex, false);

on jsfiddle

在jsfiddle

remember jsfiddle is in frames

记住,jsfiddle是帧

#4


0  

The problem with changing location.href to the current URL with a hash value is that the page won't reload but jump to the given ID.

改变位置的问题。href到当前具有散列值的URL,即页面不会重新加载,而是跳转到给定的ID。

If you really want to jump to the home div then you can just jump with location.href='index.jsp' and edit your index file to set location.href = '#home' on load.

如果你真的想要跳转到home div你可以直接跳转到location。href='index。并编辑索引文件以设置位置。href = '#home' on load。

If you want to be able to pass information across to the newly loaded page (to provide a specific id) you could use the query string instead, e.g. to jump page use location.href = 'index.jsp?loaddiv=foo

如果您希望能够将信息传递到新加载的页面(以提供特定的id),您可以使用查询字符串,例如,跳转页面使用位置。href = ' index . jsp ? loaddiv = foo

Then once the page loads read the query string and use the value to jump to the requested div, e.g. location.search = '#foo'

然后页面加载之后,读取查询字符串并使用该值跳转到所请求的div,例如location。搜索= ' # foo '

For details on extracting values from the query string see this question.

有关从查询字符串提取值的详细信息,请参见这个问题。