这是我的代码:
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 . " ";
}
}
?>
>
$ this-&gt; htmlOptions包含
array('data-edit' => 'avatar')
输出:
banner 300 avatar undefined
我不明白为什么我的上一个结果未定义。我正在尝试
priorities['avatar']; // ouput is 301
输出很好。
答案 0 :(得分:1)
问题是没有空格字符,你可以用引号改进代码包装:
<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 . "' ";
}
}
?>
请注意,我使用
更改'
(使用真实空格分隔属性)