如何用 JavaScript 打开一个新的 jsp 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10320374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:27:09  来源:igfitidea点击:

How to open a new jsp page with JavaScript

javascript

提问by lina

I have the following dropdown menu how can i redirect each menu to corresponding page by clicking each of menu? is it possible by using one javascript function if yes how? thanks in advance...

我有以下下拉菜单如何通过单击每个菜单将每个菜单重定向到相应的页面?如果可以,是否可以使用一个 javascript 函数?提前致谢...

<div>
 <ul>
  <li id=home onclick="show(this.id)"><a href="home.jsp">Home</a></li>
  <li id=collection onclick="show(this.id)><a href="collection.jsp">Collection</a></li>
     <ul>
       <li id=men onclick="show(this.id)><a href="men.jsp">Men</a></li>
       <li id=women onclick="show(this.id)><a href="women.jsp">Women</a></li>
     </ul>
  <li id=contact onclick="show(this.id)><a href="contact.jsp">Contact</a></li>
</ul>
</div>

回答by Hitham S. AlQadheeb

Yes you can.

是的你可以。

Use window.open("URL")in your case URL is this.id

window.open("URL")在您的情况下使用URL 是this.id

Also you can update window.location

你也可以更新 window.location

read more here http://www.tizag.com/javascriptT/javascriptredirect.php

在这里阅读更多http://www.tizag.com/javascriptT/javascriptredirect.php

Like Niko said you need closing quotes

就像尼科说的你需要结束语

.. onclick="window.open(this.id)" ..

回答by Elliot Bonneville

Try this?

试试这个?

$("div > ul li a").click(function() {
    window.location.href += "/" + $(this).parent()[0].id;
});

I tried to use a selector that didn't modify your HTML, so that's why it looks so icky.

我试图使用一个没有修改你的 HTML 的选择器,所以这就是为什么它看起来如此糟糕。

回答by Flavio Cysne

If you only need o JS function to redirect based on a parameter that, in your case, is the component ID, this will do the work:

如果您只需要 o JS 函数根据参数进行重定向,在您的情况下,该参数是组件 ID,这将完成以下工作:

<script type="text/javascript">
    var show = function(id) {
        window.location.href = id + '.jsp';
    };
</script>

If you want to navigate DOM and get link's href attribute use:

如果要导航 DOM 并获取链接的 href 属性,请使用:

document.getElementById(id).firstChild.href

Considering that the first element inside the component referred by ID is a link tag.

考虑到ID所引用的组件内部的第一个元素是一个链接标签。

回答by BalusC

This makes no sense.

这没有任何意义。

I understand that your particular functional requirement is to invoke the link when the enduser clicks somewhere in the space of the <li>outsidethe link. To achieve that just set the CSS displayproperty of the <a>element to block. This way the link will span the entire space of its parent element.

我了解您的特定功能要求是在最终用户单击链接<li>外部空间中的某处时调用链接。为此,只需将元素的 CSSdisplay属性设置<a>block. 这样,链接将跨越其父元素的整个空间。

<ul id="menu">
  <li><a href="home.jsp">Home</a></li>
  <li><a href="collection.jsp">Collection</a></li>
     <ul>
       <li><a href="men.jsp">Men</a></li>
       <li><a href="women.jsp">Women</a></li>
     </ul>
  <li><a href="contact.jsp">Contact</a></li>
</ul>

with this CSS

使用这个 CSS

#menu li a {
    display: block;
}

No need for ugly JavaScript hacks. Opening a new JSP page location in the current window is by the way to performed by window.location = 'some.jsp';. But this is not necessary if you use the right solution for the concrete problem. In the future questions, try to elaborate more about the functional requirement instead of concentrating only on the solution of which you thought that it's the right solution for the particular functional requirement.

不需要丑陋的 JavaScript 黑客。在当前窗口中打开一个新的 JSP 页面位置是由window.location = 'some.jsp';. 但是,如果您针对具体问题使用正确的解决方案,则这不是必需的。在未来的问题中,尝试详细说明功能需求,而不是只关注您认为它是特定功能需求的正确解决方案的解决方案。