HTML制作一个输入对象,onClick="" ”事件,转到当前网站的主机名(转到域的根)

发布于 2024-12-11 13:41:48 字数 557 浏览 0 评论 0 原文

我正在尝试编写下面的代码以将当前窗口带到域的根目录。

假设您在 http://www.example.com/myprofile/status/... 如果您单击代码中的图像,它将带您到根目录或主机名,即 <代码>http://www.example.com。

我尝试了很多方法来实现这项工作,方法太多,无法全部列出。它必须以某种方式成为可能。请帮帮我。

<input onmouseover='this.src="board-button_acti… 
onmouseout='this.src="board-button.png"'
name="submit" 
border="0" 
type="image" src="board-button.png" alt="Board Index" value="Board Index" onclick="window.location.hostname">

您要在这段代码中查看的是最后一部分,标题为 onclick="window.location.hostname"

I am trying to make this code below to take the current window to the root of the domain.

say if you are on http://www.example.com/myprofile/status/… if you click on the image in the code, it will take you to the root or host name, being http://www.example.com.

i have tried many ways to make this work, way too many ways to list them all. it has to be possible some how. please help me out.

<input onmouseover='this.src="board-button_acti… 
onmouseout='this.src="board-button.png"'
name="submit" 
border="0" 
type="image" src="board-button.png" alt="Board Index" value="Board Index" onclick="window.location.hostname">

what you want to look at in this code is the very last part, titled onclick="window.location.hostname"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-12-18 13:41:48
onclick="location.href= '/'; return false;"

(返回 false 以停止提交表单,这可能会导致导航成功时出现不一致的结果。)您还可以写入 location.pathname 来更改路径,而无需更改主机,但导航到相对 URL 也同样有效。

但是,我建议如果您有一个按钮可以将您带到不同的页面,那么您实际上在这里拥有的是一个链接。将其标记为简单的 会更合适、表现更好。如果您愿意,可以使用 CSS 对其进行样式设置,使其看起来像一个按钮。

<input onmouseover='this.src="board-button...

input type="submit" 没有 src 所以这不会做任何事情。使用 CSS :hover 样式来获得鼠标悬停效果。

onclick="location.href= '/'; return false;"

(Returning false to stop the form from being submitted, which could lead to inconsistent results on which navigation succeeds.) You can also write to location.pathname to change the path without changing host, though navigating to a relative URL is just as effective.

However, I'd suggest that if you have a button that takes you to a different page, what you actually have here is a link. It would be much more appropriate, and better-behaving, to mark it up as a simple <a href="/">. You can use CSS to style it so it looks like a button if you prefer.

<input onmouseover='this.src="board-button...

input type="submit" has no src so this won't do anything. Use CSS :hover styling to get mouse-over effects.

高跟鞋的旋律 2024-12-18 13:41:48

在你的帮助下我回答了这个问题。

我在输入周围添加了一个 href,并且刚刚摆脱了 onclick

这里是输出,并且它有效!

<a href="/">
<input onmouseover='this.src="board-button_acti… 
onmouseout='this.src="board-button.png"'
name="submit" 
border="0" 
type="image" src="board-button.png" alt="Board Index" value="Board Index" />
</a>

with your guy's help i answered the issue.

i added a href around the input, and just got rid of the onclick

here is the output, and it works!

<a href="/">
<input onmouseover='this.src="board-button_acti… 
onmouseout='this.src="board-button.png"'
name="submit" 
border="0" 
type="image" src="board-button.png" alt="Board Index" value="Board Index" />
</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文