简体   繁体   English

如何在新选项卡中打开特定的菜单div

[英]How to open particular menu div in a new tab

I have created a one page website. 我创建了一个单页网站。 When i click on menu it go to particular div tag. 当我单击菜单时,它会转到特定的div标签。 When I right click on menu & click on 'open in new tab' it opens url "www.mysite.com/#" instead it should open "www.mysite.com/#show-3". 当我右键单击菜单并单击“在新选项卡中打开”时,它会打开URL“ www.mysite.com/#”,而应打开“ www.mysite.com/#show-3”。

Please help me with this......soon 请尽快帮我...

 /**************Script used to open Different menu div on page*********************/ $(".menu a").click(function(){ var id = $(this).attr('class'); id = id.split('-'); $("#menu-container .content").hide(); $("#menu-container #menu-"+id[1]).addClass("animated fadeInDown").show(); return false; }); 
 <div class="menu-wrapper"> <ul class="menu"> <li><a class="show-1" class="MENUNAME" href="#" onclick="javascript:myFunction(this);">About Us</a></li> <li><a class="show-2" class="MENUNAME" href="#" onclick="javascript:myFunction(this);">Rooms</a></li> <li><a class="show-3" class="MENUNAME" href="#" onclick="javascript:myFunction(this);">Reservation</a></li> <li><a class="show-4" class="MENUNAME" href="#" onclick="javascript:myFunction(this);">Gallery</a></li> <li><a class="show-5" class="MENUNAME" href="#" onclick="javascript:myFunction(this);">Contact Us</a></li> </ul> <!-- /.menu --> </div> 

This will work 这会起作用

<a target="_blank" href="yourlink">Link</a>

In your case 就你而言

 <div class="menu-wrapper"> <ul class="menu"> <li><a target="_blank" class="show-1" href="#">About Us</a></li> <li><a target="_blank" class="show-2" href="#">Rooms</a></li> <li><a target="_blank" class="show-3" href="#">Reservation</a></li> <li><a target="_blank" class="show-4" href="#">Gallery</a></li> <li><a target="_blank" class="show-5" href="#">Contact Us</a></li> </ul> <!-- /.menu --> </div> 

The "open in new tab" button in most browsers will follow standard hyperlink rules, meaning that they will open the href of the tag. 大多数浏览器中的“在新标签页中打开”按钮将遵循标准的超链接规则,这意味着它们将打开标记的href。 A one page site usually manages all it's content with javascript and dynamic html generation. 一页站点通常使用javascript和动态html生成来管理所有内容。 The open in new tab function doesn't know how to handle this. 在新标签页中打开功能不知道该如何处理。

What you are going to have to do is write routing and html generation logic, give all of your links actual href attributes, not just hashtags. 您要做的是编写路由和html生成逻辑,为所有链接提供实际的href属性,而不仅仅是标签。 For normal page navigation, you can return false whenever a user clicks on a link so that you can perform your normal one page logic, but when someone forces the browser to navigate to the href location (as in opening in a new tab), you will have an actual page with your div displayed there. 对于正常的页面导航,只要用户单击链接,您就可以执行常规的一页逻辑,但您可以返回false,但是当有人强迫浏览器导航到href位置时(如在新标签页中打开),您可以将会有一个实际的页面,其中显示了您的div。

Please Try this fiddle Demo 请尝试这个小提琴演示

Just make minor change in your a tag 只需对标签做些小改动

<a href="#" target="_blank">

You can give divs id: 您可以给div ID:

<div id="show-1">About Us Content</div>

And create hyperlinks like: 并创建如下超链接:

<a class="show-1" target="_blank" href="yourPage.html#show-1">About Us</a></li>

This will open your div directly in new tab. 这将直接在新标签中打开div。

The thing about what you are trying to do is that, if you want to open just that particular div in a new web page, it isn't a 1 page site anymore. 关于您要执行的操作的问题是,如果您只想在新网页中打开特定的div,则它不再是1页的网站。 It is a NEW page with that div content. 这是具有该div内容的新页面。

Regarding your code, the 关于您的代码,

... href="your new page link here"

that " # " of yours is the link (or in a more technical term, it will append that link to your base url on your current page. Which resulted on it going to "www.mysite.com/#" The reason why it shows the same page instead of just a new page is simply because # is not to link to a new page but to an element in your current page on any event (such as an onclick). You can get more details on that here 您的“#”是链接(或更确切地说,它将将该链接附加到您当前页面上的基本URL中。导致该链接转到“ www.mysite.com/#”显示同一页面而不是仅显示一个新页面,仅仅是因为#在任何事件(例如onclick)上,不是链接到新页面而是链接到当前页面中的元素,您可以在此处获取更多详细信息

But like the other answers had mentioned, if you want to open a new tab (aka page), you have to have another page. 但是,就像前面提到的其他答案一样,如果要打开新标签页(又称页面),则必须有另一个页面。 Which also means this will no longer be a 1 page website. 这也意味着这将不再是一个1页的网站。

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

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