javascript - Add CSS styles with PrototypeJS - Stack Overflow

I have a div that I want to add a new style to using PrototypeJS. But the following script does not wor

I have a div that I want to add a new style to using PrototypeJS. But the following script does not work. What is wrong with it?

<div class='exercise-main'>
</div>

$$('.exercise-main').setStyle({
    backgroundColor: '#900',
    fontSize: '12px'
});

I have a div that I want to add a new style to using PrototypeJS. But the following script does not work. What is wrong with it?

<div class='exercise-main'>
</div>

$$('.exercise-main').setStyle({
    backgroundColor: '#900',
    fontSize: '12px'
});
Share Improve this question edited Jun 4, 2012 at 21:39 James Allardice 166k22 gold badges334 silver badges315 bronze badges asked May 30, 2012 at 6:21 user1077300user1077300 4234 gold badges10 silver badges18 bronze badges 1
  • If my answer helped out out, I'd appreciate it if you accepted my answer. Thanks! ( ^ _ ^ ) – dontGoPlastic Commented Aug 2, 2012 at 20:56
Add a ment  | 

1 Answer 1

Reset to default 4

The $$ function returns an array of all matching elements (if any). setStyle is an element method, not an array method.

There are a few options here.

If you're only ever expecting a single element to be matched, this will work.

$$('.exercise-main')[0].setStyle({color:'red'});

If you want to set the style on every matched element, either of these will work. They're essentially the same.

$$('.exercise-main').each(function(element) {
    element.setStyle({color: 'red'});
});

or

$$('.exercise-main').invoke('setStyle', {color:'red'});

Of course, if your element has an id, you can use the $ function to find just that element. This differs from $$ in that you don't pass it a selector string, but just the string of the id (ie no '#'). Also, this returns just the element or null - not an array - so you can use element methods straight from the returned value, similar to what you were initially attempting.

$('myElementId').setStyle({color:'red'});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745520140a4631222.html

相关推荐

  • javascript - Add CSS styles with PrototypeJS - Stack Overflow

    I have a div that I want to add a new style to using PrototypeJS. But the following script does not wor

    3天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信