简体   繁体   English

尝试向jQuery对象添加HTML类属性

[英]Trying to add an HTML class attribute to jQuery object

I'm trying to get the class name from an HTML div elements and add it to an existing object. 我正在尝试从HTML div元素中获取类名,并将其添加到现有对象中。 What's maddening is that I can make it work, but every syntax error checker I put it into says the code is wrong. 令我发狂的是我可以使它工作,但我说的每个语法错误检查都说代码是错误的。 I've searched pretty thoroughly, but I don't even know how to phrase the question, really. 我搜索得非常彻底,但我甚至不知道该怎么说这个问题。 Here's the code which DOESN'T throw the error: 这是不抛出错误的代码:

$.each( $(objDiv).children('div'), function(){
    var field = $(this).attr('class');
    var value = $(this).text();
    var add = { field : value };
    $.extend(obj, add);
});

But the problem is that it puts "field" in as a literal string for the field 但问题是它将“字段”作为字段的字符串

object {label: "level3a", field : "Authoritatively enhance client-centric deliverables after sustainable expertise."} object {label:“level3a”, field :“在可持续专业知识之后,以权威方式增强以客户为中心的可交付成果。”}

object {label: "level3a", field: "Completely embrace clicks-and-mortar data without ubiquitous meta-services."} object {label:“level3a”, field: “完全接受点击实体数据而无需普遍存在的元服务。”}

not the class name value. 不是类名值。 To get around that, I put brackets around "field": 为了解决这个问题,我将括号放在“字段”周围:

$.each( $(objDiv).children('div'), function(){
    var field = $(this).attr('class');
    var value = $(this).text();
    var add = { [field] : value };
    $.extend(obj, add);
}

And it gives me the result I'm looking for, 它给了我正在寻找的结果,

object {label: "level3a", ClassA : "Authoritatively enhance client-centric deliverables after sustainable expertise."} object {label:“level3a”, ClassA :“在可持续专业知识之后,以权威方式增强以客户为中心的可交付成果。”}

object {label: "level3a", ClassB: "Completely embrace clicks-and-mortar data without ubiquitous meta-services."} object {label:“level3a”, ClassB: “完全接受点击实体数据而无需普遍存在的元服务。”}

but somehow it's incorrect. 但不知何故,这是不正确的。 Any ideas on what I'm doing wrong here, or a better way to accomplish this? 关于我在这里做错了什么的想法,或者更好的方法来实现这个目标?

While I can't tell you why that's wrong (or why it works, for that matter), I can tell you a more "proper" way to do it. 虽然我不能告诉你为什么那是错的(或者为什么它有效),我可以告诉你一个更“正确”的方法来做到这一点。

You'd declare add as an empty object like so: add = {}; 您将add为空对象,如下所示: add = {};

Then you can add the field as a property to it like so: add[field] = value; 然后你可以像这样添加字段作为属性: add[field] = value;

This will give you an object add with the model: { ClassA: "Your text here" } 这将为您提供模型的对象add{ ClassA: "Your text here" }

Please refer to the types of property accessors for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors . 有关详细信息,请参阅属性访问者的类型: https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors The method detailed above uses "Bracket Notation" 上面详述的方法使用“Bracket Notation”

Edit: So apparently in ECMA6 what you wrote is valid javascript, using what's called a ComputedPropertyName . 编辑:显然在ECMA6中你所写的是有效的javascript,使用的是所谓的ComputedPropertyName However it's not supported by all browsers yet, so until browser support for ECMA6 gets better you're better off using the bracket-notation property name method. 然而,所有浏览器都不支持它,因此在ECMA6的浏览器支持变得更好之前,最好使用括号 - 表示法属性名称方法。 See this answer for more info: https://stackoverflow.com/a/2274327/2748503 有关详细信息,请参阅此答案: https//stackoverflow.com/a/2274327/2748503

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM