hourArray = []
var hour9 = $("#9").attr("id") var hour10 = $("#10").attr("id") var hour11 = $("#11").attr("id") var hour12 = $("#12").attr("id") var hour13 = $("#13").attr("id") var hour14 = $("#14").attr("id") var hour15 = $("#15").attr("id") var hour16 = $("#16").attr("id") var hour17 = $("#17").attr("id")
var hour9N = parseInt(hour9) var hour10N = parseInt(hour10) var hour11N = parseInt(hour11) var hour12N = parseInt(hour12) var hour13N = parseInt(hour13) var hour14N = parseInt(hour14) var hour15N = parseInt(hour15) var hour16N = parseInt(hour16) var hour17N = parseInt(hour17)
hourArray.push(hour9N) hourArray.push(hour10N) hourArray.push(hour11N) hourArray.push(hour12N) hourArray.push(hour13N) hourArray.push(hour14N) hourArray.push(hour15N) hourArray.push(hour16N) hourArray.push(hour17N)
for (i = 0; i < hourArray.length; i++) {
if (hourArray[i] === hour) {
var color = $(hourArray[i]).toString();
$(color).removeClass("row time-block")
else if (hourArray[i] < hour) {
var color = $(hourArray[i]).toString()
else {
var color = $(hourArray[i]).toString()
}
I am trying to convert my integers back into string data so that I may target the appropriate members of the array. It works fine when I console log the data (console logs the appropriate message for the proper number of members of said array for each condition). But when I try to put the variable in the add/remove class method, it throws up an error saying unrecognized expression [object OBJECT]. Could someone please point me in the right direction?
我正在尝试将整数转换回字符串数据,以便可以针对数组的适当成员。当我在控制台记录数据时,它工作得很好(控制台为每个条件记录适当数量的数组成员的适当消息)。但是当我试图把变量放在add/remove类方法中时,它抛出一个错误,说是unrecognized expression [object OBSTRUCTURE]。有人能给我指个路吗?
Ive attempted to convert the integers back to strings but it does not seem to work for the add/remove class method.
我尝试将整数转换回字符串,但它似乎不适用于添加/删除类方法。
更多回答
优秀答案推荐
With this you are taking an integer and passing it to the jQuery constructor.
I assume you meant to concat it with a '#' in order to get a CSS selector for the id (see http://api.jquery.com/id-selector/). I don't know what you're trying to achieve with toString
. You are converting the jQuery element which is pointing at the DOM element and throwing it away and replacing it with a String. A string is fine for logging, but no good for calling jQuery methods on.
这样,你就可以获取一个整数并将其传递给jQuery构造函数。我假设你的意思是用一个'#'来连接它,以便获得一个用于id的CSS选择器(请参阅http://api.jquery.com/id-selector/)。我不知道你想用toString实现什么。您正在转换指向DOM元素的jQuery元素,并将其丢弃并将其替换为String。字符串对于日志记录来说很好,但是对于调用jQuery方法来说就不好了。
var color = $(hourArray[i]).toString();
Probably want:
可能想要:
var color = $('#' + hourArray[i]);
color.removeClass("row time-block")
You could also simplify this if you wanted by using a class on all your objects and going through them with each loop. Also, keep in mind that an ID that is just a number isn't considered valid if you're trying to meet HTML standards. if you want to store info on an element a custom data attribute might be your ideal solution.
如果需要,还可以通过在所有对象上使用一个类并在每个循环中遍历它们来简化这一过程。此外,请记住,如果您试图满足HTML标准,则只是一个数字的ID不会被视为有效。如果您希望存储有关元素的信息,则自定义数据属性可能是您理想的解决方案。
Check this: Use data attributes
检查此选项:使用数据属性
const hourArray = $('.hour');
hourArray.each(function (index) {
var offset = index + 9; //this is only needed because you start at nine
if (offset === hour) {
$(this).removeClass("row time-block");
} else if (offset < hour) {
//offset will be your color
} else {
//else stuff
}
});
更多回答
That is exactly what I am trying to do. Thank you for the clarification. I completely forgot to add back the #. If that is the case, I dont need to string it, do i? Or can I string it, then add the #. Or would it be better to just concat the # to the integer.
这正是我正在努力做的事情。谢谢你的澄清。我完全忘了加回#。如果是这样的话,我不需要把它串起来,对吗?或者我可以把它串起来,然后加上#。或者更好的做法是将#连接到整数。
Thank you. I just saw the second added part. Thank you very much!!!
谢谢。我刚刚看到了第二个增加的部分。非常感谢!
我是一名优秀的程序员,十分优秀!