在javascript中动态设置变量

时间:2017-01-12 17:24:56

标签: javascript jquery

尝试根据页面上有多少vimeo iframe动态设置变量。我在下面的代码中使用Eval方法:

<EligibilityRecords xmlns="http://Eligibility_LSDD">
   <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>553739837</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>DOE</PT_LST_NM>
      <PT_FRST_NM>JANE</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_PLAN_TYPE>Family</EN_PLAN_TYPE>
      <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT/>
      <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
      <Dependent_Record>
         <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
         <DP_DEPENDENT_FILE_IMP_ID>NC110012077673</DP_DEPENDENT_FILE_IMP_ID>
         <DP_RELATIONSHIP>Spouse</DP_RELATIONSHIP>
         <DP_LST_NM>DOE</DP_LST_NM>
         <DP_FRST_NM>JOHN</DP_FRST_NM>
      </Dependent_Record>
      <Dependent_Record>
         <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
         <DP_DEPENDENT_FILE_IMP_ID>NC110012077680</DP_DEPENDENT_FILE_IMP_ID>
         <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
         <DP_LST_NM>DOE</DP_LST_NM>
         <DP_FRST_NM>JIMMY</DP_FRST_NM>
      </Dependent_Record>
      <Dependent_Record>
         <DP_PARTICIPANT_FILE_IMP_ID>553739837</DP_PARTICIPANT_FILE_IMP_ID>
         <DP_DEPENDENT_FILE_IMP_ID>NC110012077681</DP_DEPENDENT_FILE_IMP_ID>
         <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
         <DP_LST_NM>DOE</DP_LST_NM>
         <DP_FRST_NM>JESSICA</DP_FRST_NM>
      </Dependent_Record>
   </Eligibility_Detail_Record>
   <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>610051908</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>JACKSON</PT_LST_NM>
      <PT_FRST_NM>MICHAEL</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_PLAN_TYPE>IndSpouse</EN_PLAN_TYPE>
      <EN_MBRSHP_EFF_STRT_DT>01012017</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT/>
      <EN_PLAN_YEAR_STRT_DT>01012017</EN_PLAN_YEAR_STRT_DT>
      <Dependent_Record>
         <DP_PARTICIPANT_FILE_IMP_ID>610051908</DP_PARTICIPANT_FILE_IMP_ID>
         <DP_DEPENDENT_FILE_IMP_ID>NC110015202761</DP_DEPENDENT_FILE_IMP_ID>
         <DP_RELATIONSHIP>Spouse</DP_RELATIONSHIP>
         <DP_LST_NM>JACKSON</DP_LST_NM>
         <DP_FRST_NM>MELISSA</DP_FRST_NM>
      </Dependent_Record>
   </Eligibility_Detail_Record>
   <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>561859010</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>MCLEOD</PT_LST_NM>
      <PT_FRST_NM>CONOR</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_PLAN_TYPE>IndChild</EN_PLAN_TYPE>
      <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT/>
      <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
      <Dependent_Record>
         <DP_PARTICIPANT_FILE_IMP_ID>561859010</DP_PARTICIPANT_FILE_IMP_ID>
         <DP_DEPENDENT_FILE_IMP_ID>SC000018241906</DP_DEPENDENT_FILE_IMP_ID>
         <DP_RELATIONSHIP>Dependent</DP_RELATIONSHIP>
         <DP_LST_NM>MCLEOD</DP_LST_NM>
         <DP_FRST_NM>DUNCAN</DP_FRST_NM>
      </Dependent_Record>
   </Eligibility_Detail_Record>
   <Eligibility_Detail_Record xmlns="">
      <PT_PARTICIPANT_FILE_IMP_ID>573794953</PT_PARTICIPANT_FILE_IMP_ID>
      <PT_LST_NM>GABRIEL</PT_LST_NM>
      <PT_FRST_NM>PETER</PT_FRST_NM>
      <EN_PLAN_NAME>Health Reimbursement Arrangement</EN_PLAN_NAME>
      <EN_PLAN_TYPE>Ind</EN_PLAN_TYPE>
      <EN_MBRSHP_EFF_STRT_DT>01012016</EN_MBRSHP_EFF_STRT_DT>
      <EN_MBRSHP_EFF_END_DT/>
      <EN_PLAN_YEAR_STRT_DT>01012016</EN_PLAN_YEAR_STRT_DT>
   </Eligibility_Detail_Record>
</EligibilityRecords>

然而,我的评估行会生成错误消息:

  

未捕获的SyntaxError:意外的标识符

对我来说,它看起来像是连接正确,所以不确定我哪里出错了?

1 个答案:

答案 0 :(得分:1)

即使在这种情况下我认为不是那么糟糕,但一般意见是根本不使用 eval 。 改为使用数组:

var numberVimeoFrames = jQuery(".vimeo").length;

var players = [];
for(i=1;i<=numberVimeoFrames;i++){
    var refFrame = jQuery('.vimeo:nth-child(' + i + ')');
    players.push(new Vimeo.Player(refFrame));
}

您现在可以通过调用阵列来访问您的玩家(例如玩家[1] 而不是 player1 等等。)