Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当按钮聚焦时更改链接_Javascript_If Statement_Button_Hyperlink - Fatal编程技术网

Javascript 当按钮聚焦时更改链接

Javascript 当按钮聚焦时更改链接,javascript,if-statement,button,hyperlink,Javascript,If Statement,Button,Hyperlink,基本上,我将使用一个应用程序风格的移动视图网站,他们希望底部有一个按钮可以按顺序浏览页面,而顶部的导航允许您按任何顺序浏览页面。我想将底部按钮更改为链接到下一页,具体取决于它所在的页面。因此,我希望按钮在特定按钮处于焦点时更改链接。我尝试了许多不同的方法,但似乎都无法奏效。请施展你的魔法 if(document.getElementById("abt").hasFocus()) { document.getElementById("golink").href = "#work"; }我不相信f

基本上,我将使用一个应用程序风格的移动视图网站,他们希望底部有一个按钮可以按顺序浏览页面,而顶部的导航允许您按任何顺序浏览页面。我想将底部按钮更改为链接到下一页,具体取决于它所在的页面。因此,我希望按钮在特定按钮处于焦点时更改链接。我尝试了许多不同的方法,但似乎都无法奏效。请施展你的魔法

if(document.getElementById("abt").hasFocus()) {
document.getElementById("golink").href = "#work";

}

我不相信
focus
是您想要的,因为focus可以快速跟踪表单对象之外的对象

我已经模拟出一个可能的解决方案来帮助你找到你想要的东西

以下是指向JSFIDLE的链接:

编辑-更新的小提琴:

小提琴中的Javascript:

// Array of sections.
var sections = [
  '#first',
  '#second',
  '#third',
  '#fourth',
  '#fifth'
]

// Get the last value in the array.
var lastArrayValue = sections[sections.length - 1];

// Button pointer
var btn = document.querySelector('#button');

// On click of the button, do work.
btn.onclick = function() {
    // Get the current href.
  var href = btn.getAttribute('href');

  // Ternary operator setting the index to zero if the user is on the last index of array.
  // Else set the index to the next array value.
  var index = (href === lastArrayValue) ? 0 : sections.indexOf(href) + 1;

  // Set the href attribute.
  btn.setAttribute('href', sections[index]);
}
注意:虽然这个例子有效,但这只是一个模型——这意味着它不能解释用户快速点击按钮来“翻页”


祝你好运,我希望这能帮助你实现目标。

你知道什么是重点吗?这只是用户的交互将被定向到任何具有焦点的对象,直到用户更改焦点或模糊事件发生。将其更改为
href
的链接与
http://xwz.com
当焦点集中且链接始终有指向
xyz.com的href时
?用户不会仅仅通过使用它来触发焦点事件吗?我猜有一种方法是通过触发器,但这听起来和前一种想法一样无用。或者,除了缺少一个按钮之外,我还缺少什么?我正在尝试更改1个按钮的链接,而另一个按钮是focusedOk,这更有意义,但有比聚焦更好的方法,除非您使用表单或制表符很重要。