简体   繁体   English

将类添加到span元素不起作用

[英]Adding class to span element is not working

I am receiving a large JSON object having large volume of data in ajax success method but when i am iterating over the JSON list and want add individual class to each element things are not working. 我在ajax成功方法中收到一个包含大量数据的大型JSON对象,但是当我在JSON列表上进行迭代并希望为​​每个元素添加单个类时,事情无法正常工作。

The code snippet i am pasting here is inside ajax success method 我在这里粘贴的代码片段是ajax成功方法

$.each(data, function(index,jsonObject){
    console.log(jsonObject.spanId+":"+jsonObject.status)
    if(jsonObject.status != null && jsonObject.status != undefined){
        if(jsonObject.status == "Available"){
        $('#'+jsonObject.spanId).addClass("availableTime");
        }else{
        $('#'+jsonObject.spanId).addClass("vacantTime");
        }
    }
});

I have tried lot but not able to accomplish, please help me. 我已经尝试了很多但无法完成,请帮助我。

I am adding chunk of my json object 我正在添加我的json对象的大块

[
    {
        "spanId": null,
        "status": null
    },
    {
        "spanId": null,
        "status": null
    },
    {
        "spanId": "25_31_15:00",
        "status": "Available"
    },
    {
        "spanId": "25_31_15:30",
        "status": "Available"
    },
    {
        "spanId": "25_31_16:00",
        "status": "Available"
    },
    {
        "spanId": "25_31_16:30",
        "status": "Available"
    },
    {
        "spanId": "25_31_17:00",
        "status": "Available"
    }
]

You span ids are not advised and it may be what is not working. 不建议跨越ids,它可能是不起作用的。

Plus : for selectors is a reserved word for pseudo classes. 另外:对于选择器是伪类的保留字。

Source 资源

Contrary to what you might expect, the use of colons in ids is okay, though only HTML5 started to accept ids starting with a digit. 与你可能期望的相反,在id中使用冒号是可以的,尽管只有HTML5开始接受以数字开头的id。 For better interoperability, it's recommended to start ids with a letter and not use colons in the first place. 为了获得更好的互操作性,建议首先使用字母启动ID,而不是首先使用冒号。

That said, if you must use colons in your identifiers, you must escape them when you use them as part of a selector: 也就是说,如果您必须在标识符中使用冒号,则必须在将它们用作选择器的一部分时将它们转义:

$('25_31_17\\:00') // this works fine

Alternatively, if you're going to find elements by their identifier, you might also want to consider: 或者,如果您要按标识符查找元素,您可能还需要考虑:

$(document.getElementById(jsonObject.spanId)) // this will work too

H i, just missing a couple of things and data is in the wrong place ( by the looks ) 我,只是遗漏了一些东西和数据是在错误的地方(通过外观)

try running this : 试试这个:

  $.each(data, function(index){
      console.log(data[index].spanId+":"+data[index].status)

  });

notice that you need to tell the loop which array item to reference : 请注意,您需要告诉循环引用哪个数组项:

  data[index]

and that the 'each' is looping over the array: 并且'each'循环遍历数组:

   $.each(data, function(index){ ... 

EDIT with data ref: 使用数据编辑编辑:

  var data =  [
   {
    "spanId": null,
    "status": null
   },
   {
    "spanId": null,
    "status": null
   },
   {
    "spanId": "25_31_15:00",
    "status": "Available"
   },
   {
    "spanId": "25_31_15:30",
    "status": "Available"
   },
   {
    "spanId": "25_31_16:00",
    "status": "Available"
   },
   {
    "spanId": "25_31_16:30",
    "status": "Available"
    },
    {
    "spanId": "25_31_17:00",
    "status": "Available"
    }
   ]

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

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