简体   繁体   English

如何<span>根据条件使用javascript</span>为html标签( <span>)</span>添加类

[英]How to add a class for html tag (<span>) depending a condition with javascript

I need to add style classes according to the number of <span> in a <li> I start with the following code: 我需要根据<li><span>的数量添加样式类,我从以下代码开始:

$(document).ready(function() {
    var ul = document.getElementsByTagName("ul");
for (var i, li; li = ul.childNodes[i]; i++) {
    if (li.tagName == 'LI') {
        var container = li.getElementsByTagName("span");
        var nb = container.length;
            if (nb == 1) 
        {
            container[0].childNodes[0].setAttribute("class", "class1")
        } 
            else if (nb == 2) 
        {
            container[0].childNodes[0].setAttribute("class", "class2")
            container[0].childNodes[1].setAttribute("class", "class3")
        }
    }
}});

JsFiddle Example JsFiddle示例

You are already using jQuery so I'm going to use that by itself. 您已经在使用jQuery,所以我将单独使用它。 A lot easier to do and easier to read... 更容易做,更容易阅读...

$(document).ready(function() {
    var ul = $('ul');

    var lis = ul.find('li');

    lis.each(function(){
        var spans = $(this).find('span');
        if(spans.length == 1){
            spans.addClass('class1');
        }
        if(spans.length == 2){
            $(this).find('span:first-child').addClass('class2').next('span').addClass('class3');
        }
    });    
});

http://jsfiddle.net/yXyCk/135/ http://jsfiddle.net/yXyCk/135/

Edit 编辑

I'm not sure if you need to cover all cases of # of span s in any given li . 我不确定您是否需要涵盖任何给定lispan #的所有情况。

If you do... just replace the above with.. 如果您这样做...只需将以上内容替换为..

$(document).ready(function() {
    var ul = $('ul');

    var lis = ul.find('li');

    lis.each(function(){
        var spans = $(this).find('span');
        spans.each(function(index){
            $(this).addClass('class'+(index+1));
        });
    });    
});

http://jsfiddle.net/yXyCk/137/ http://jsfiddle.net/yXyCk/137/

The above will cover all cases assuming you want to increment by 1. If you need a more specific range please let me know. 上面将涵盖所有情况,假设您希望增加1。如果需要更具体的范围,请告诉我。

As you're already using jQuery this can be done slightly more elegant :) 由于您已经在使用jQuery,因此可以稍微优雅一些​​:)

$(document).ready(function () {
    $("ul li").each(function () {
        // bug/feature: this will also add classes for elements greater 2
        $(this).find("span").each(function(index) {    
            this.className = "class" + (index + 1);
        });
    });
});

http://jsfiddle.net/mj4s7smk/ http://jsfiddle.net/mj4s7smk/

I mean, this isn't a perfect way of doing it, but since you're already using jQuery, why not go all the way: http://jsfiddle.net/4oqss5rc/ 我的意思是,这不是一种完美的方法,但是由于您已经在使用jQuery,为什么不一直使用它: http : //jsfiddle.net/4oqss5rc/

You find your list, and iterate through each of the children li elements. 您会找到列表,并遍历每个子li元素。 You check the amount of children and apply your classes. 您检查孩子的数量并申请课程。

$(document).ready(function() { 
    var $ul = $('ul');
    $ul.find('li').each(function() {
        var l = $(this).children('span').length;
        if(l == 1) {
            $(this).children(':eq(0)').addClass('class1');
        } else if(l == 2) {
            $(this).children(':eq(0)').addClass('class2');
            $(this).children(':eq(1)').addClass('class3');               
        }
    });
});

Do you mean you want to change the class of the span depending on the number of spans contained in a li? 您是说要根据li中包含的跨度数来更改跨度的类吗? For instance, three spans-> class1, class2, class3, four spans class1, class2, class3, class4 and so on? 例如,三个范围-> class1,class2,class3,四个范围class1,class2,class3,class4等等?

If that is so, check out this fiddle . 如果是这样,请检查这个小提琴 Here's the js: 这是js:

$.each($("ul li"), function (index, item) {
    var counter = 1;
    $.each($("span", item), function (ind, ite) {
        $(ite).addClass("class" + counter);
        counter += 1;
    });
});

You can use following 您可以使用以下

$(document).ready(function() {
$('li').each(function(){
  var $this = $(this);
  $this.children("span").each(function(index){
    $(this).addClass("class"+ (index+1));
  });
});
});

Fiddle link 小提琴链接

$(document).ready(function() {

    //$("li").children().first().css("color","red")
    alert($("li span").length);
    $( "li" ).each(function( index ) {
        if($("span",this).length == 1){
            $("span",this).addClass("class1");
        }else{
            $("span:nth-child(1)",this).addClass("class2");
            $("span:nth-child(2)",this).addClass("class3");
        }
    });

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

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