Javascript 如何根据通过函数传递的参数更改Jquery选择器

Javascript 如何根据通过函数传递的参数更改Jquery选择器,javascript,jquery,html,css,arrays,Javascript,Jquery,Html,Css,Arrays,我有一个功能: function factCheck(index) { if (arrayOfSites[index].indexOf("pdf") > -1) { $('#20').attr('style', 'color: red'); $('#' + index).attr('style', 'color: red'); console.log('index: ' + index);

我有一个功能:

 function factCheck(index) {

        if (arrayOfSites[index].indexOf("pdf") > -1) {


            $('#20').attr('style', 'color: red');
            $('#' + index).attr('style', 'color: red');

            console.log('index: ' + index);
            console.log($("#" + index).text());

        }
    }
function factCheck(index) {

        if (arrayOfSites[index].indexOf("pdf") > -1) {


            $('#20').attr('style', 'color: red');
            $('#' + index.toString()).attr('style', 'color: red');

            console.log('index: ' + index);
            console.log($( "#" + index.toString() ).text());

        }
    }
所以我的问题是。当我使用$('#20')时,元素的文本颜色会改变颜色,但当我使用,$('#'+索引)时,它不起作用。 有趣的是,我用console.log。。它记录元素的文本,但我不能影响它的css

为什么会这样

//经过三个小时的会议。。我带着一些非常棒的答案回来了!!谢谢

编辑: 下面的代码显示了我如何抓取页面上的所有链接,并添加与该项目索引相等的id。这就是为什么我试图抓住这个链接,并以某种方式实现它。我很感激你们。。我想我将在字符串通过函数进入时,在字符串中添加一个字母,然后从该点操纵锚点。我只是想知道是否有更有效的方法

  $(".lpage a").each(function (index) {

        // console.log(index + ": " + $(this).text());
        str = $(this).attr('href');
        arrayOfSites.push(str);
        str = arrayOfSites[index];

        title = $(this).attr('title');
        parseURL(str);

        $('.colContent2').append(cellOpen + '<a onclick="whichFunction(' + index + ');" id= "' + index + '"style="cursor:pointer;" class="injectedLinkCol2" >' + str + '</a>' + cellClose).prop("id", index);

    });
$(“.lpage a”)。每个(函数(索引){
//log(index+”:“+$(this.text());
str=$(this.attr('href');
推送(str);
str=阵列岩[index];
title=$(this.attr('title');
parseURL(str);
$('.colContent2').append(cellOpen+''+str+''+cellClose).prop(“id”,index);
});

尝试使用
toString()
函数:

 function factCheck(index) {

        if (arrayOfSites[index].indexOf("pdf") > -1) {


            $('#20').attr('style', 'color: red');
            $('#' + index).attr('style', 'color: red');

            console.log('index: ' + index);
            console.log($("#" + index).text());

        }
    }
function factCheck(index) {

        if (arrayOfSites[index].indexOf("pdf") > -1) {


            $('#20').attr('style', 'color: red');
            $('#' + index.toString()).attr('style', 'color: red');

            console.log('index: ' + index);
            console.log($( "#" + index.toString() ).text());

        }
    }

也许它与id属性的名称有关。看看这个。

id名称或类名必须以下划线(ux)、连字符(-)或字母(a–z)开头

大概是

'#d20'
#20{
    color: red;
}
会有用的


见此:

我无法重现确切的问题。我做了一支钢笔,试了你要的东西,但效果很好。所以在剩下的代码中一定是有错误

相关说明

在CSS中,id不允许以数字开头(允许类)。所以写一些像

'#d20'
#20{
    color: red;
}
不起作用,但该规则仅适用于css。JQuery仍然有效,这意味着您唯一的选择是编写内联样式或使用JQuery的
.attr
.css
,但是
JQuery.attr()
将重置所有内联样式。剩下的就是使用
.css()
。所以,最好不要以数字开头

尝试使用
.css
而不是
.attr
,看看它是否有效。

$('.exampleClass:eq('+index+')).css(“颜色”,“黄色”)

出于某种原因,它起作用了

$('.exampleClas').eq(index).css(“颜色”、“黄色”)


不起作用。

我无法重现该问题。您能创建一个堆栈片段或JSFIDLE吗?当连接一个字符串时,它被精确地调用