简体   繁体   English

将构造的类名传递给JQuery

[英]Pass a constructed class name into JQuery

I'm using an array to append to a base class name so I can iterate through different classes. 我正在使用数组附加到基类名称,以便我可以遍历不同的类。

First bit works fine. 第一位工作正常。 Get a count on the array and as long as I exclude the last two lines of code I get all the desired class names in the console. 计算数组,只要我排除最后两行代码,我就可以在控制台中获得所有需要的类名。

But once I add the last two lines the loop and try to add the constructed class name to get the different text values per class I get the following error: 但是,一旦我将最后两行添加到循环并尝试添加构造的类名以获取每个类的不同文本值,我会收到以下错误:

Uncaught Error: Syntax error, unrecognized expression: '.moduleStatusDIS' 未捕获错误:语法错误,无法识别的表达式:'。moduleStatusDIS'

And it stops at the first iteration through the array. 并且它在第一次迭代时停止通过数组。 Code below. 代码如下。

function setModuleStatusColour() {

var array = ["DIS", "DDG", "CDX", "DKM", "DBV", "DBB", "DGK", "DAM", "LOG", "DUS", "DCL", "DRI"];
var arrayLength = array.length;
console.log(arrayLength);
for (x=0; x < arrayLength; x++){

    var className = "'"+'.moduleStatus'+array[x]+"'";
    console.log(className);
    var statusValue = $(className).text();
    console.log(statusValue);
}

} }

I also tried to use eval() (although I know it's been deprecated, desperation got the better of me) see below. 我也尝试使用eval()(虽然我知道它已经被弃用了,绝望对我有好处)见下文。 Same result as code above though. 与上面的代码相同的结果。

function setModuleStatusColour() {

var array = ["DIS", "DDG", "CDX", "DKM", "DBV", "DBB", "DGK", "DAM", "LOG", "DUS", "DCL", "DRI"];
var arrayLength = array.length;
console.log(arrayLength);
for (x=0; x < arrayLength; x++){

    var className = "'"+'.moduleStatus'+array[x]+"'";
    console.log(className);
    eval('var statusValue = $(className).text()');
    console.log(statusValue);       
}

} }

Even tried to cast the var className into a String but didn work for me either. 甚至试图将var className强制转换为String,但对我来说也没有用。

If I write it out class by class and don't use an array to construct the names it works fine but I tried to keep the code short and make it easy to add. 如果我逐类写出它并且不使用数组来构造名称它工作正常但我试图保持代码简短并使其易于添加。 So it has become a matter of principle :) 所以它已成为一个原则问题:)

Any help would be greatly appreciated! 任何帮助将不胜感激!

This line: 这一行:

var className = "'"+'.moduleStatus'+array[x]+"'";

should be simply 应该简单

var className = ".moduleStatus" + array[x];

giving you a selector like 给你一个选择器

var statusValue = $(".moduleStatusDIS").text();

Selectors are just strings, there is no need to wrap it in single quotes. 选择器只是字符串,不需要用单引号将其包装起来。

It should be easy, just: 它应该很简单,只需:

function setModuleStatusColour() {
    var array = ["DIS", "DDG", "CDX", "DKM", "DBV", "DBB", "DGK", "DAM", "LOG", "DUS", "DCL", "DRI"];
    var arrayLength = array.length;
    console.log(arrayLength);
    for (x=0; x < arrayLength; x++){
        var statusValue = $('.moduleStatus'+array[x]).text();
        console.log(statusValue);
    }

Selectors are conformed by: $(-string-) where -string- should be the name of a class preceded by a dot (".className"), or the name of an element like $("div") (but this will select all div elements!), or the id of an element preceded by a # symbol like $("#sendButton")... The reason why .className and #sendButton are surrounded by quotation is that they are strings that refer to attributes given to elements like so: 选择器符合:$( - string-)其中-string-应该是以点(“.className”)开头的类的名称,或者是$(“div”)之类的元素的名称(但这将是选择所有div元素!),或前面带#符号的元素的id,如$(“#sendButton”)... .className和#sendButton被引号括起来的原因是它们是引用属性的字符串给予像这样的元素:

<div class="className">...</div> --> $(".className")
<div id="sendButton">..<div> --> $("#sendButton")
<div>...</div> --> $("div")

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

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