jQuery创建对象 [英] jQuery creating objects

查看:79
本文介绍了jQuery创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何在jQuery中创建一个对象,然后继续创建该对象的几个不同实例,例如

How would I create an object in jQuery, and then proceed to make a couple of different instances of this object I.e

创建一个名为box的对象,其中包含一个名为color的变量.

Create an object named box which holds a variable called color.

然后使用不同的存储颜色创建该对象的几个实例.

And then make a couple of instances of this object with different stored colours.

推荐答案

使用JQueryJavascript中制作对象,从 dom 获取数据并将其传递给对象 Box ,例如,将它们存储在 Boxs 的数组中,可以是:

Another way to make objects in Javascript using JQuery, getting data from the dom and pass it to the object Box and, for example, store them in an array of Boxes, could be:

var box = {}; // my object
var boxes =  []; // my array

$('div.test').each(function (index, value) {
    color = $('p', this).attr('color');
    box = {
        _color: color // being _color a property of `box`
    }
    boxes.push(box);
});

希望有帮助!

这篇关于jQuery创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆