简体   繁体   English

在新选项卡中打开结果(Javascript)

[英]Open results in new tab (Javascript)

I got an dynamic page with results, and the problem is that the links appearing there won't open on a new tab, this is the code but i am not being able to sort it out.我得到了一个带有结果的动态页面,问题是出现在那里的链接不会在新选项卡上打开,这是代码,但我无法对其进行整理。

I would like that when a page (link) is clicked to have it opened on a new tab instead of going direct to the site.我希望当单击页面(链接)以在新选项卡上打开它而不是直接访问该站点时。

BTW: English is not my native language, i hope i was able to describe it right.顺便说一句:英语不是我的母语,我希望我能够正确地描述它。

   var updateResultsHtml = function() 
{
      for ( var i = 0, exists = 0, elements = []; i < results.length; i++ ) {
         exists += (statuses.success.indexOf(results[i].status) >= 0 ? 1 : 0);
         elements.push(appendChildren(createElement('div', ['class', 'col3']), [
            createElement('a', ['href', results[i].url, 'data-value', results[i].message], results[i].url.replace(/(https?\:\/\/)?(www\.)?/i, ''))
         ]));
      }
      resultsUsername.innerText   = username;
      resultsUsername2.innerText  = username;
      resultsPercentage.innerText = Math.round((exists / total) * 100) + '%';
      removeChildren(resultsParent);
      appendChildren(resultsParent, elements);
      togglePanel(resultsPanel);
   };

   var getExistence = function() {
      httpRequest('POST', 'existence.php', ['content-type', 'application/x-www-form-urlencoded'], 'index=' + index + '&username=' + username, function(data) {
         if ( index < total ) {
            ++index;
            results.push(JSON.parse(data));
            updateProccessingHtml();
            getExistence();
         }
         else {
            updateResultsHtml();
         }
      });
   };

Also my other question is related to the submit button, it has to be clicked in which case the code won't recognize Enter button, what would be the change to make it recognize the enter and make it submittable also when Enter is pressed.另外我的另一个问题与提交按钮有关,必须单击它,在这种情况下,代码将无法识别 Enter 按钮,要使其识别 Enter 并在按下 Enter 时也使其可提交有什么变化。

submitButton.onclick = function() {
  index    = 0;
  results  = [];
  username = usernameInput.value;
  if ( username.length > 0 ) {
     window.location.href   = '//' + window.location.host + window.location.pathname + '#' + username;
     usernameInput.disabled = true;
     submitButton.disabled  = true;
     getExistence();
  }

You can force your hyperlinks to open in a new tab using the attributed-text target attribute.您可以使用属性文本target属性强制在新选项卡中打开超链接。

Target specifies where to display the linked URL.目标指定在哪里显示链接的 URL。 It is a name of, or keyword for, a browsing context: a tab, window, or iframe.它是浏览上下文的名称或关键字:选项卡、窗口或 iframe。


Syntax:句法:

<a target="_blank|_self|_parent|_top|framename">


Attribute Values属性值

The following keywords have special meanings:以下关键字具有特殊含义:

  • _self : Load the URL into the same browsing context as the current one. _self :将 URL 加载到与当前浏览器相同的浏览上下文中。 This is the default behavior.这是默认行为。

  • _blank : Load the URL into a new browsing context. _blank :将 URL 加载到新的浏览上下文中。 This is usually a tab, but users can configure browsers to use new windows instead.这通常是一个选项卡,但用户可以将浏览器配置为使用新窗口。

  • _parent : Load the URL into the parent browsing context of the current one. _parent :将 URL 加载到当前浏览器的父浏览上下文中。 If there is no parent, this behaves the same way as _self.如果没有父对象,则其行为方式与 _self 相同。

  • _top : Load the URL into the top-level browsing context (that is, the "highest" browsing context that is an ancestor of the current one, and has no parent). _top :将 URL 加载到顶级浏览上下文(即“最高”浏览上下文,它是当前浏览上下文的祖先,并且没有父浏览上下文)。 If there is no parent, this behaves the same way as _self.如果没有父对象,则其行为方式与 _self 相同。


Modifying your example to open links in a new tab修改您的示例以在新选项卡中打开链接

You can simply add the target attribute to your createElement function arguments, as follows:您可以简单地将target属性添加到createElement函数参数中,如下所示:

createElement('a', ['href', results[i].url, 'target', '_blank', 'data-value', results[i].message], results[i].url.replace(/(https?\:\/\/)?(www\.)?/i, ''))

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

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