douguachi0056
2017-12-09 07:27
浏览 77

如何在循环变量的jQuery中应用css?

Hello guys I search alot but find nothing regarding my problem. I have one foreach loop in that i have one anchor tag and that will be changed according to my ifelse condition. Now I want to change color of each if else condition in that. Please help me to do.My code is within jQuery script.

for(var j=0;j<arrayData.length;j++){
  var country = "";
  if(arrayData[j].key=="40"){
    country = "UK";
  }else if(arrayData[j].key=="41"){
    country = "INDIA";
  }else if(arrayData[j].key=="47"){
    country = "POLAND";
  }else{
    country = "GERMANY";
  }
  check+='<a class="btn btn-success" href="<?php echo base_url()?>uploads/'+arrayData[j].value+'">'+country+
                    '</a>|';
}
check +='</td></tr>';

图片转代码服务由CSDN问答提供 功能建议

大家好我搜索了很多但没有发现我的问题。 我有一个foreach循环,我有一个锚 标签,这将根据我的 ifelse 条件进行更改。 现在我想更改每个if else条件的颜色。 请帮我做。我的代码在jQuery脚本中。

  for(var j = 0; j&lt; arrayData.length; j ++){
 var country =“”  ; 
 if(arrayData [j] .key ==“40”){
 country =“UK”; 
} else if(arrayData [j] .key ==“41”){
 country =“  INDIA“; 
} else if(arrayData [j] .key ==”47“){
 country =”POLAND“; 
} else {
 country =”GERMANY“; 
} 
 check + =  '&lt; a class =“btn btn-success”href =“&lt;?php echo base_url()?&gt; uploads /'+ arrayData [j] .value +'”&gt;'+ country + 
'&lt; / a&gt  ; |'; 
} 
check + ='&lt; / td&gt;&lt; / tr&gt;'; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtxf759200 2017-12-09 07:34
    已采纳

    You can just append it on the html string.

    for(var j=0;j<arrayData.length;j++){
       var country = "";
       var color = "";
       if(arrayData[j].key=="40"){
           country = "UK";
           color = "red";
       }else if(arrayData[j].key=="41"){
           country = "INDIA";
           color = "orange";
       }else  if(arrayData[j].key=="47"){
           country = "POLAND";
           color = "yellow";
       }else{
            country = "GERMANY";
           color = "green";
       }
    
       check+='<a class="btn btn-success" style="background-color:' + color  + ';" href="<?php echo base_url()?>uploads/'+arrayData[j].value+'">'+country+
                        '</a>|';
    }
    
     check +='</td></tr>';
    

    Other option: You can also add class to <a> tag.

    Like <a class="color-red">

    CSS:

    .color-red {
       background-color: red;
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题