简体   繁体   English

如何根据Javascript中的url向div添加类?

[英]How to add class to div depending on url in Javascript?

How do I add a class to a specific class on a specific page using pure Javascript? 如何使用纯Javascript将类添加到特定页面上的特定类? I've played around with a number of different code-snippets but can't manage to get anything to work. 我玩过很多不同的代码片段,但是无法使任何东西正常工作。 I'm looking for something like the following: 我正在寻找类似以下内容的东西:

<script>
    if (window.location.href == 'http://specific-page.com') {
      $(somecode).find('existingClass').addClass('newClass');
   };
</script>

You can use querySelectorAll plus NodeList.forEach() . 您可以使用querySelectorAll加上NodeList.forEach()

Moreover, you can take a look to the Element.classList methods . 此外,您可以看一下Element.classList方法

The snippet: 片段:

 // // For test purposes only // window.location.href1 = 'http://specific-page.com'; if (window.location.href1 == 'http://specific-page.com') { document.querySelectorAll('#myDiv .existingClass').forEach(function(ele, idx) { ele.classList.add('newClass'); }); }; 
 .existingClass { height: 30px; background-color: red; } .newClass { border-style: dotted; } 
 <div id="myDiv"> <p class="existingClass">Thbis is a sample string</p> </div> 

It should work: 它应该工作:

<script>
  if (window.location.href == 'http://specific-page.com') {
    $('.existingClass').addClass('newClass');
  };
</script>

What you have to do is to use the class you are looking for as a selector, in this case it is ".existingClass" . 您要做的是将要查找的类用作选择器,在本例中为".existingClass"

There are many other ways to do it depending on what you are trying to acchive. 还有许多其他方法可以完成此操作,具体取决于您要尝试获取的内容。

Other post: 其他职位:

jquery change class name jQuery的更改类名

jQuery changing css class to div jQuery将CSS类更改为div

how to change class name of an element by jquery 如何通过jQuery更改元素的类名称

this is the easiest but get elements by classname will fail on some browsers like older ie 这是最简单的方法,但是按类名获取元素将在某些浏览器(如较旧的浏览器)上失败

compatibility can be found here - https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName#Browser_compatibility 兼容性可以在这里找到-https: //developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName#Browser_compatibility

 // JavaScript Document

window.onload = init;
var divsWithClass = null;


function init() {
    if (window.location.href.indexOf("some value you care about") >= 0) {
        this.divsWithClass = document.getElementsByClassName("Some Class Name");
        var index = 0;
        while (index < divsWithClass.length) {
            divsWithClass[index].className += " newClass";
            index++;
        }
    }
    console.log(divsWithClass);
}

You can use the setAttribute to set the Attribute class to a new value. 您可以使用setAttributeAttribute 设置为新值。 In the value, you can add the new class to the existing class value. 在该值中,您可以将新类添加到现有的类值中。 Also, the getElementsByClassName returns the Elements list, so you need to select the right element Appropriately. 另外, getElementsByClassName返回Elements列表,因此您需要适当地选择正确的元素。 I am using the element at first index below. 我在下面的第一个索引处使用元素。

var elements = document.getElementsByClassName("existingClass");
elements[0].setAttribute( "class", "existingClass" + " newClass" );

If you want to do this in Vannila JS, then you can use the code from below. 如果要在Vannila JS中执行此操作,则可以使用下面的代码。 It doesn't use jQuery. 它不使用jQuery。

 if ( window.location.href == 'http://specific-page.com' ) { var el = document.getElementsByClassName("currentClass")[0]; var newClass = el.getAttribute("class"); newClass += " myNewClass"; el.setAttribute("class", newClass) console.log(el) } 

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

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