简体   繁体   English

Javascript / jQuery-将CSS样式应用于类元素

[英]Javascript/jQuery - apply CSS style to class element

Im trying to insert CSS style to a h1 class element. 我试图将CSS样式插入h1类元素。 It worked before i updated mmy theme, but for some reason it doesnt work now. 它在我更新mmy主题之前起作用,但是由于某种原因,它现在不起作用。 Anyone know why? 有人知道为什么吗?

I've tried getElementsByClassName and [0] to find arrays, nothing is working for me. 我已经尝试过getElementsByClassName和[0]来查找数组,但对我来说没有任何用。 The CSS class has display:none, because i want the title to only show on pages that contains the class "product_title entry-title". CSS类具有display:none,因为我希望标题仅显示在包含“ product_title entry-title”类的页面上。 So when it find that class, it should put a CSS class to the element to display:block, so that it will show on those pages. 因此,当找到该类时,应将CSS类放置到display:block元素上,以便在这些页面上显示。

 z=document.querySelector("product_title entry-title"); z.style.display="block"; 
 #content h1:first-child { padding-top: 0px; display:none; } 
 <h1 class="product_title entry-title">Arkham Horror</h1> 

You are missing dot . 您缺少点. while selecting the element using querySelector.Dot . 同时使用querySelector.Dot选择元素. is required to select the element using it's class. 必须使用其类来选择元素。

Also one class will be enough to select the element 一类也足以选择元素

 var z = document.querySelector(".product_title"); z.style.display = "block"; 
 #content h1:first-child { padding-top: 0px; display: none; } 
 <section id='content'> <h1 class="product_title entry-title">Arkham Horror</h1> </section> 

You need to declare your variable first. 您需要先声明您的变量。 Also, querySelectorAll works the same way as CSS selectors so you'll need to add dots to your classes for querySelector to find them. 另外,querySelectorAll与CSS选择器的工作方式相同,因此您需要在类中添加点,以便querySelector可以找到它们。

 var z = document.querySelector(".product_title.entry-title");
 z.style.display="block";

Bear in mind this will select only the first H1 with those classes so your :first-child is a bit overkill here. 请记住,这只会选择那些类别的第一个H1,因此您的:first-child有点过分了。

You seem to have misunderstood the use of the querySelector method. 您似乎误解了querySelector方法的使用。 Instead of just listing the classes of the desired element all delimited by a space, each class name must be preceded by a full stop . 不仅要列出所需元素的类,所有类都用空格分隔,而且每个类名都必须以句号开头. and if your desired element has more than one class, the classes should not have whitespace inbetween them. 并且如果您想要的元素具有多个类,则这些类之间不应包含空格。 See the corrected code snippet: 请参阅更正的代码段:

 var z=document.querySelector(".product_title.entry-title"); z.style.display="block"; 
 h1 { padding-top: 0px; display:none; } 
 <h1 class="product_title entry-title">Arkham Horror</h1> 

Use jQuery .css() method for changing css properties. 使用jQuery .css()方法更改CSS属性。 It will apply to all elements which were chosen by selector (in this case it is 'h1') If you want to change only one element add .first() 它适用于选择器选择的所有元素(在本例中为“ h1”)。如果只想更改一个元素,请添加.first()

 $('h1').first().css("color", "red"); $('h1').css("display", "block"); 
 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <h1>Title</h1> 

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

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