英文:
Javascript object value returning undefined with string key
问题
Sure, here's the translated code:
所以,我试图根据其他元素的属性来更新元素的类。我能够正确获取所需的值,例如
alignment = "start"
但是,当我尝试从对象中获取其关联的值时,它返回undefined,可能的原因是什么。此外,是否有更好的方法来实现这一目标。
// 更新对齐按钮
var alignmentMap = {
"end": "bi-text-right",
"start": "bi-text-left",
"center": "bi-text-center",
"justify": "bi-justify"
}
var alignment = $(activeId + " .component-content").css("text-align");
console.log(alignmentMap[alignment]);
alignment = alignmentMap[alignment];
$("#cp-align .settings-container i").attr("class", "bi " + alignment);
I've translated the code part and removed the unnecessary instructions.
英文:
So, I'm trying to update the class of an element based on the properties of some other element. I am able to correctly get the required value, for instance
alignment = "start"
But when I try to get its associated value from the object, it returns undefined, what might be the possible reasons for that. Also ,is there a better way to achieve this.
//Update Alignment Button
var alignmentMap = {
"end": "bi-text-right",
"start": "bi-text-left",
"center": "bi-text-center",
"justify": "bi-justify"
}
var alignment = $(activeId+" .component-content").css("text-align");
console.log(alignmentMap[alignment]);
alignment = alignmentMap[alignment];
$("#cp-align .settings-container i").attr("class", "bi "+alignment);
答案1
得分: 0
以下是代码中需要翻译的部分:
-
Available text-align property in CSS are
left
,right
,center
,justify
. 如果 text-align CSS 属性设置为text-align: left
,那么alignment
的值应为left
,并且alignmentMap
对象中相应的键应为left
,而不是 start。对于right
也是一样的。 -
var alignmentMap = {
"right": "bi-text-right",
"left": "bi-text-left",
"center": "bi-text-center",
"justify": "bi-justify"
} -
var alignment = $(activeId+ " .component-content").css("text-align");
-
console.log(alignmentMap[alignment]);
-
alignment = alignmentMap[alignment];
-
$("#cp-align .settings-container i").attr("class", "bi " +alignment);
英文:
Available text-align property in CSS are left
, right
, center
, justify
. If the text-align CSS property is set to text-alight: left
, then the alignment
value should be left
and corresponding key in the alignmentMap
object should be left
, not start. Same for right
as well.
//Update Alignment Button
var alignmentMap = {
"right": "bi-text-right",
"left": "bi-text-left",
"center": "bi-text-center",
"justify": "bi-justify"
}
var alignment = $(activeId+" .component-content").css("text-align");
console.log(alignmentMap[alignment]);
alignment = alignmentMap[alignment];
$("#cp-align .settings-container i").attr("class", "bi "+alignment);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论