Style properties in html

I”ve got a class with the display set lớn none I”d lượt thích to in Javascript now phối it lớn inline I”m aware I can vì this with an id with getElementById but what”s the cleanest way to do it with a class?


you can create new style rule instead.

Bạn đang xem: Style properties in html

var cssStyle = document.createElement(“style”);cssStyle.type = “text/css”;var rules = document.createTextNode(“.YOU_CLASS_NAMEdisplay:hidden”);cssStyle.appendChild(rules);document.getElementsByTagName(“head”).appendChild(cssStyle);$(“#YOUR_DOM_ID”).addClass(“YOUR_CLASS_NAME”); You may lượt thích khổng lồ exploit/rewrite this function:

function getStyleRule(ruleClass, property, cssFile) { for (var s = 0; s to scan all style sheets attached pass “” as third argument, otherwise something like “index.css”ruleClass contains starting “.”don”t forget to lớn use javascript syntax over css properties, e.g. box-shadow vs. boxShadow Although this is long gone, here a few remarks:

Using display: inline to make things visible again may spoil thepage flow. Some elements are displayed inline, others blochồng etc. Thisshould be preserved. Hence, only define a .hidden style và remove itto make things visible again.

How to lớn hide: There are (at least) two ways khổng lồ hide elements, one isthe above mentioned display: none which basically makes the elementbehave sầu as if it was not there, và the visibility: hidden whichrenders the element invisible but keeps the space it occupies.Depending on what you want to hide, the visibility may be a betterchoice, as other elements will not move sầu when showing/hiding anelement.

Xem thêm: phần mềm lvvl

Adding/removing classes vs. manipulating CSS rules: The result isquite different. If you manipulate the CSS rules, all elements havinga certain CSS class are affected – now & in the future, i.e. newelements dynamically added lớn the DOM are also hidden, whereas whenyou add/remove sầu a class, you must make sure that newly added elementsalso have sầu the class added/removed. So, I”d say adding/removingclasses works well for static HTML, whereas manipulating CSS rulesmight be a better choice for dynamically created DOM elements.

Trả lời Hủy

Email của bạn sẽ không được hiển thị công khai minh bạch. Các ngôi trường phải được khắc ghi *

Bình luận

Tên *

Email *

Trang website

Lưu tên của tôi, email, với trang web vào trình ưng chuẩn này mang lại lần comment kế tiếp của tôi.