当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - prototype programmatic style sheet

问题描述:

Excuse the naivete of this but I have defined styles and upon a button click I would like to assign a style to an element. The target element is "msg00" and I cannot determine what attribute of that to pick to assign the desired style sheet.

$$('id="msg00"').style="start"; knowable wrong. Also how would this be different in jQuery? Which I could migrate to.

网友答案:

$$('id="msg00"') is bad syntax. The preferred way of finding a single element by an ID is with $

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

The general way of finding elements by their attribute is with an attribute selector. But this returns an array so you cannot access it's style property, or any other, directly. You would have to resort to any of the Array or Enumerable methods.

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

Now to assign a style to an element which has been found you can use the convenient Element#setStyle method.

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

As an example here is the same effect but for all msg* elements.

// this selector means an ID which starts with "msg"
$$('[id^=msg]').invoke('setStyle', {
    backgroundColor: 'red'
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: