如何使用js中设置的变量作为参数名称来设置属性?

时间:2015-06-16 12:01:18

标签: javascript

我需要做的是动态设置属性名称。我会告诉你我正在尝试的代码:

 for (i = 0; i < $nrOfCriteriiReducere; i++)
  {

       var eroareText = "eroareIDCazReducere" + " "  + i;
       var eroareHtmlClass = ".eroareCazReducere" + i;


        if (raspuns.eroriProprietar.eroareText)
         {
             $(eroareHtmlClass).css("display", "inline");
             $(eroareHtmlClass).text(raspuns.eroriProprietar.eroareText);
          }
    }

它没有用,它给了我:

  

Uncaught SyntaxError:意外的字符串错误。

我怎样才能做到这一点?

  

解决方案:

这是解决方案(我也通过动态设置name属性而不是类来改变它):

for (i = 0; i < $nrOfCriteriiReducere; i++)
{

       var eroareText = "eroareIDCazReducere" + " " + i;
       var eroareName = "eroareCazReducere" + i;

       if (raspuns.eroriProprietar[eroareText])
       {

             var name = "[name=" + eroareName + "]";
             $(name).css("display", "inline");
             $(name).text(raspuns.eroriProprietar[eroareText]);
       }
 }

0 个答案:

没有答案