简体   繁体   English

href 未链接到外部 url

[英]href not linking to external url

I downloaded a one pager template which is designed very strange.我下载了一个设计很奇怪的单页机模板。 All hyperlinks are linking to sections on the page and it is using a data-link="" parameter.所有超链接都链接到页面上的部分,并且使用 data-link="" 参数。

Now I try to link one of the menu items to an external url with target _blank, but then it is looking for the url that I defined on the page itself (so it is looking for the section which is not there eg www.example.com/ http://google.nl )现在我尝试将其中一个菜单项链接到带有目标_blank的外部url,但随后它正在寻找我在页面本身上定义的url(因此它正在寻找不存在的部分,例如www.example。 com/ http://google.nl

The links has a value on the href elements like this:链接在 href 元素上有一个值,如下所示:

 href="/home/" or href="/menu/" <ul id="w1" class="navbar-nav nav"> <li><a id="navhome" class="" href="/home" title="Home" data-link="home">Home</a></li> <li><a id="navmenu" class="" href="/menu" title="Menu" data-link="menu">Menu</a></li>

So this doesn't work (also without the target _blank:所以这不起作用(也没有目标_blank:

 <li><a id="navorder" href="http://order.company.com" target="_blank">Order Now</a></li>

What technology is used here?这里使用了什么技术? And how can I link a new menu item to a external page?以及如何将新菜单项链接到外部页面?

EDIT:编辑:

I found the code that is handling the href:我找到了处理 href 的代码:

 $(".menu ul li a").not('.social-link').click(function(e) { e.preventDefault(); $(".menu ul li a").removeClass("active"); tabTarget = $(this).data('link'); animateByMenu = true; var hash = '#' + tabTarget; var url = $(this).prop('href'); var menuItem = $(this).data('link'); $(".menu li a[data-link=" + menuItem + "]").addClass("active"); if (url) { var top = $(hash).offset().top - 60; $('html, body').animate({ scrollTop: top }, 600, function(){ }); history.pushState('', '', url); $('.navbar-toggle:visible').click(); } return false; });

And when I click the link it is giving me the following error in console:当我单击链接时,它在控制台中给了我以下错误:

 main.js:109 Uncaught TypeError: Cannot read property 'top' of undefined at HTMLAnchorElement.<anonymous> (main.js:109) at HTMLAnchorElement.dispatch (jquery.min.js:3) at HTMLAnchorElement.r.handle (jquery.min.js:3)

检查附带的JS代码,可能模板正在使用标记进行一些操作。

There should be a piece of code capturing all "a" clicks and prevents them for their default behavior.应该有一段代码捕获所有“a”点击并阻止它们的默认行为。 Remove that data attribute and use simple href.删除该数据属性并使用简单的href。 That should work.那应该行得通。

If the coder was so strict, then the solution above might not work.如果编码器如此严格,那么上面的解决方案可能不起作用。 Then you should do some gymnastics like using a div maybe and attach a click handler to it.然后你应该做一些体操,比如使用 div 并附加一个点击处理程序。

So since we don't know what kinda template you use you could use this:因此,由于我们不知道您使用哪种模板,您可以使用:

 <ul id="w1" class="navbar-nav nav"> <li><a id="navhome" class="" href="/home" title="Home" data-link="home">Home</a></li> <li><a onclick="window.open('http://www.google.com', '_blank')">Menu</a></li> </ul>

This will redirect you to another page in a new tab.这会将您重定向到新选项卡中的另一个页面。 Hope this works for you, it's just a simple workaround.希望这对您有用,这只是一个简单的解决方法。

The most obvious thing that is happening is that JavaScript captures all links (perhaps with a more specific selector), prevents the default action (ie follow the link) and scrolls to the section.发生的最明显的事情是 JavaScript 捕获所有链接(可能使用更具体的选择器),阻止默认操作(即跟随链接)并滚动到该部分。 Something like this, in jQuery.像这样的东西,在 jQuery 中。

 $("a").each(function(i, el) { const $el = $(el); $el.click(function(e) { const href = $el.attr("href"); // Find an element that has href as an ID // Scroll to that element $("html, body").scrollTop($("#" + href).offset().top); // Prevent default action, ie, don't follow the hyperlink // to a new location e.preventDefault(); }); });

Following your edit.按照您的编辑。 Firstly, that's some bad JS imo.首先,这是一些糟糕的 JS imo。 But on topic: if you want to have some links to actually open a new page, add a new class to them.但是关于主题:如果您想要一些链接来实际打开一个新页面,请向它们添加一个新的 class 。 For instance, use a class external for external links.例如,外部链接使用 class external链接。

 <li><a id="navorder" href="http://order.company.com" target="_blank" class="external">Order Now</a></li>

and change the first line of the jQuery snippet as follows, so that the click handler is not attached to your .external elements.并将 jQuery 片段的第一行更改如下,以便点击处理程序不附加到您的.external元素。

 $(".menu ul li a").not('.social-link,.external').click(function(e) {

Ok, in this line:好的,在这一行:

 $(".menu ul li a").not('.social-link').click(function(e) {

You could add a class to your links (.external-link) and remove from the selection:您可以将 class 添加到您的链接 (.external-link) 并从选择中删除:

 $(".menu ul li a").not('.social-link').not('.external-link').click(function(e) {

Or you could be a little more clever and try to check if the link is external based on the href (the code only look for http, not https):或者你可以更聪明一点,尝试根据 href 检查链接是否是外部的(代码只查找 http,而不是 https):

 $(".menu ul li a").not('.social-link').click(function(e) { if (e.target.href.indexOf('http://').== 0) { e;preventDefault(). }..

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

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