未定义但存在

Undefined but existing

这是我的代码:

js

var priorities = {
  banner: '300',
  avatar: '301'
};
var editableDiv = $('*[data-edit]');

editableDiv.append('<div class="overlay-edit"></div>');
editableDiv.each(function(index, value) {
  var actualPriorities = value.getAttribute("data-edit");

  console.log(actualPriorities);
  console.log(priorities[actualPriorities]);
});

html/php

<div class="hexagon-wrap" style="height:<?php echo $this->outterHeight;?>px; width:<?php echo $this->outterWidth;?>px"
<?php
if($this->htmlOptions) {
    foreach ($this->htmlOptions as $key => $value) {
        echo $key . "=" . $value . "&nbsp";
    }
}
?>
>

$this->htmlOptions 包含

array('data-edit' => 'avatar')

输出:

banner
300
avatar 
undefined

我不明白为什么我最后的结果是不确定的。当我尝试

priorities['avatar']; // ouput is 301

输出不错

问题是没有中断 space 字符,您可以改进引号中的代码换行:

<div class="hexagon-wrap" style="height:<?php echo $this->outterHeight;?>px; width:<?php echo $this->outterWidth;?>px"
<?php
if($this->htmlOptions) {
    foreach ($this->htmlOptions as $key => $value) {
        echo $key . "='" . $value . "' ";
    }
}
?>

请注意,我将 &nbsp; 更改为 '(使用真正的 space 来分隔属性)