Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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:带有document.activeElement的变量_Javascript_Variables - Fatal编程技术网

Javascript:带有document.activeElement的变量

Javascript:带有document.activeElement的变量,javascript,variables,Javascript,Variables,我想创建一个变量,使我的代码更通用。以下是我正在编写的代码: eventHandler: function(event) { if (event.keyCode == KEY_DOWN) { if (this.hasNextSibling(document.activeElement.parentNode)) { this.setFocus(document.activeElement.parentNode.nextSibling.firstCh

我想创建一个变量,使我的代码更通用。以下是我正在编写的代码:

eventHandler: function(event) {
    if (event.keyCode == KEY_DOWN) {
        if (this.hasNextSibling(document.activeElement.parentNode)) {
             this.setFocus(document.activeElement.parentNode.nextSibling.firstChild);
         } else {
              this.debug("out");
         }
    }
},
setFocus: function(item)
{
document.activeElement.tabIndex = "-1";
item.focus();
document.activeElement.tabIndex = "0";
},
...
问题出在“document.activeElement.parentNode.nextSibling.firstChild”部分。为了能够重用代码,我首先要做的是设置如下变量:

var test = document.activeElement.parentNode.nextSibling.firstChild; /*CHANGE HERE*/
eventHandler: function(event) {
    if (event.keyCode == KEY_DOWN) {
        if (this.hasNextSibling(document.activeElement.parentNode)) {
             this.setFocus(test); /*CHANGE HERE*/
         } else {
              this.debug("out");
         }
    }
},
setFocus: function(item)
{
document.activeElement.tabIndex = "-1";
item.focus();
document.activeElement.tabIndex = "0";
},
...
代码不编译。我尝试了许多不同的方法,比如方法引用,但似乎没有任何效果。有人有主意吗

非常感谢

编辑: 以下是我真正想要的:

var x = parentNode.nextSibling.firstChild;
eventHandler: function(event) {
    if (event.keyCode == KEY_DOWN) {
        if (this.hasNextSibling(parent)) {
             this.setFocus(document.activeElement.x);
         } else {
              this.debug("out");
         }
    }
},
setFocus: function(item)
{
document.activeElement.tabIndex = "-1";
item.focus();
document.activeElement.tabIndex = "0";
},
语义不正确。我有办法做到吗?
谢谢

,因为只要有按键,activeElement就不一样了,所以没有必要将它保留在功能之外。 对于你编辑过的答案,我唯一能想到的是 你的代码应该是

var x = "parentNode.nextSibling.firstChild";//edited here
eventHandler: function(event) {
    if (event.keyCode == KEY_DOWN) {
        if (this.hasNextSibling(parent)) {
             this.setFocus(eval("document.activeElement." + x));//edited here
         } else {
              this.debug("out");
         }
    }
},
setFocus: function(item)
{
document.activeElement.tabIndex = "-1";
item.focus();
document.activeElement.tabIndex = "0";
},

问题是您试图在对象体中声明一个变量,其中只有键值对是有效的。尝试:

test : document.activeElement.parentNode.nextSibling.firstChild,
eventHandler: function(event) {
    if (event.keyCode == KEY_DOWN) {
        if (this.hasNextSibling(document.activeElement.parentNode)) {
             this.setFocus(this.test); /*CHANGE HERE*/
         } else {
              this.debug("out");
         }
    }
},
...

代码看起来像是对象文字的一部分。你的代码在语法上不适合那里。谢谢,这正是我想要的!