简体   繁体   English

使用原型JS动态添加CSS

[英]Add CSS Dynamically Using Prototype JS

I would like to add CSS to an HTML Element dynamically using Prototype JS. 我想使用Prototype JS将CSS动态添加到HTML元素中。 I am using below code. 我正在使用下面的代码。

var wrapper_content = $('wrapper').innerHTML;
wrapper_content.img.setStyle({display:'inline'});

But this is not working. 但这是行不通的。

In cases like these you can use the invoke() utility method like so 在这种情况下,您可以像这样使用invoke()实用程序方法

$$('#wrapper img').invoke('setStyle',{'display':'inline'});

http://api.prototypejs.org/language/Enumerable/prototype/invoke/ http://api.prototypejs.org/language/Enumerable/prototype/invoke/

You could change your selector to var wrapper_content = $('wrapper img'); 您可以将选择器更改为var wrapper_content = $('wrapper img');

And then you can do like this wrapper_content.each(function(el) { el.setStyle({ 'display': 'inline' }); }); 然后,您可以像这样wrapper_content.each(function(el) { el.setStyle({ 'display': 'inline' }); });

Edit: Changed the syntax to work with Prototype JS instead. 编辑:更改了语法,以代替Prototype JS。

Seems to me you are asking about adding a style to images that are children of the element with the id 'wrapper': 在我看来,您正在询问是否要为ID为'wrapper'的元素的子级图像添加样式:

$('wrapper').select('img').each(function(name,index) {
  name.style.display = "inline";
});

Ive been using a combination of Geek Num 88 and T Gibbons answers for a few years. 几年来,我一直在使用Geek Num 88和T Gibbons答案。 its a combination of css selectors and setting the style display value is more obvious. 它结合了CSS选择器和设置样式显示值更加明显。

 $$('#wrapper img').each(function(name) { name.style.display = "inline"; }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.3/prototype.js"></script> <div id="wrapper"> <img style="display: none;" src="https://cdn0.iconfinder.com/data/icons/free-business-desktop-icons/128/Home.png" /> </div> 

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

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