简体   繁体   English

如何使用JavaScript打开新的JSP页面

[英]How to open a new jsp page with JavaScript

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? 是否可以使用一个javascript函数(是)? thanks in advance... 提前致谢...

<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>

Yes you can. 是的你可以。

Use window.open("URL") in your case URL is this.id 如果您的URL是this.id ,请使用window.open("URL")

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 就像Niko所说的,您需要关闭引号

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

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的选择器,所以这就是为什么它看起来如此讨厌的原因。

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: 如果您只需要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引用的组件内部的第一个元素是链接标记。

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> outside the link. 我了解您的特定功能要求是,当最终用户单击链接外部 <li>空间中的某个位置时,调用链接。 To achieve that just set the CSS display property of the <a> element to block . 为此,只需将<a>元素的CSS display属性设置为block This way the link will span the entire space of its parent element. 这样,链接将跨越其父元素的整个空间。

<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. 无需难看的JavaScript技巧。 Opening a new JSP page location in the current window is by the way to performed by window.location = 'some.jsp'; 通过在window.location = 'some.jsp';执行的方式在当前窗口中打开新的JSP页面位置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. 在以后的问题中,请尝试详细说明功能需求,而不是仅专注于您认为对特定功能需求而言是正确解决方案的解决方案。

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

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