admin管理员组文章数量:1394979
Is it possible to define a class with specific style attributes dynamically with jQuery, rather than setting the style of all elements with that class?
I could set the attributes of the class at the end of the script once all the elements have been created, but is that the best way to go about it? If I define the style of the class with $('.class').css('property','value');
at the beginning of the script, nothing would happen because the elements with class .class
haven't been created yet, right?
Is it possible to define a class with specific style attributes dynamically with jQuery, rather than setting the style of all elements with that class?
I could set the attributes of the class at the end of the script once all the elements have been created, but is that the best way to go about it? If I define the style of the class with $('.class').css('property','value');
at the beginning of the script, nothing would happen because the elements with class .class
haven't been created yet, right?
3 Answers
Reset to default 6The proper way to do that is to use CSS. If you want elements with class "foo" to have a bunch of properties, then put those properties in a CSS file and associate them with an appropriate selector:
.yourClass {
font-weight: bold;
color: green;
}
If you need to do this dynamically, it's possible to write CSS blocks with Javascript. Put a <style>
element in your page and give it an "id" value:
<style id='dynamicStyle'></style>
Then you can set it with jQuery:
$('#dynamicStyle').text(".yourClass { font-weight: bold; }");
If I'm understanding you, you want to add style to specific elements.
You don't have to use .css(a,b)
to do this. jQuery has a function called addClass("className");
that will add a class to whatever element you want.
$('.originalClass').addClass('addedClass');
It doesn't overwrite the original.
Is that what you wanted?
EDIT:
Or are you saying that you want to modify your stylesheet in javascript?
If that's the case, there are javascript rules for modify stylesheet properties.
If you (for some reason) can't change the stylesheet, then maybe you could simply store your styles in an object, and apply that.
var stylesToAdd = { background:"orange", border:"1px solid blue" }
And use .css()
to apply them.
$('#myElement').css(stylesToAdd);
Not sure what you're reasoning for this is, but yes - you have to call the script once the elements have been created. If you don't want to have the script at the end of your document, wrap it in
$(document).ready(function() {
...
});
or, alternatively, the shorthand
$(function() {
...
});
So it fires once all the HTML is loaded.
本文标签: javascriptDefining a class with specific style dynamically in jQueryStack Overflow
版权声明:本文标题:javascript - Defining a class with specific style dynamically in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744104103a2590985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论