原型程序化样式表

时间:2014-09-30 21:03:13

标签: javascript css prototypejs

请原谅我的天真,但我已经定义了样式,点击按钮后我想为一个元素指定一个样式。目标元素是"msg00",我无法确定要选择哪个属性来分配所需的样式表。 $$('id="msg00"').style="start";可知错误。另外在jQuery中会有什么不同?我可以迁移到哪个。

1 个答案:

答案 0 :(得分:0)

$$('id="msg00"')语法错误。通过ID查找单个元素的首选方法是使用$

$('msg00') // returns a single element

按属性查找元素的一般方法是使用attribute selector。但是这会返回一个数组,因此您无法直接访问它的style属性或任何其他属性。您必须使用任何ArrayEnumerable方法。

$$('[id=msg00]') // returns an array of elements

现在要为找到的元素指定样式,可以使用方便的Element#setStyle方法。

$('msg00').setStyle({
    backgroundColor: 'red'
});

这里的示例与所有msg*元素的效果相同。

// this selector means an ID which starts with "msg"
$$('[id^=msg]').invoke('setStyle', {
    backgroundColor: 'red'
});