本文介绍了我们可以使用变量在JAVASCRIPT中声明div吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Phone Gap-android.我想根据记录的长度动态设置SWIPE VIEW.

I am working in Phone Gap-android.I wanted to set a SWIPE VIEW dynamically based on the length of the records.

我该怎么做?长时间后,我尝试实现以下代码.对还是错?

How do i do that? After long time,I tried implementing the following code.Am I right or Wrong?

value= VALUE_FROM_DB.split("||");
            for (k=0;k<value.length;k++)
            {
                if (value[0] == paramName1)
                {
                      return unescape(value[k]);
                      console.log("no of swipe views ");
                  }
                   var val = k+1;
                  var ni = document.getElementById('swiper-wrapper');
                  var newdiv = document.createElement('div');
                  var divIdName = 'swiper-slide'+val;
                  console.log("div name: "+divIdName);
                  newdiv.setAttribute('id',divIdName);
                  newdiv.setAttribute('class','swiper-slide');
                  var cnt1 = '<div id="container'+val+'"><span><img src="img/abc'+val+'.png" style="float:left; " /></span><div id="abc'+val+'"><span><h5>'+value[k]+'</h5></span></div></div>';
        ---->     console.log("div_id :"+id);
                  document.getElementById(+divIdName).innerHTML=cnt1;
                  console.log("value_from_db:: "+value[k]);
                  ni.appendChild(newdiv);

在尝试执行该值时,该值不打印,并且我看到一个错误,称为REFERENCE ERROR,未定义ID,并且它是空白屏幕,必须在其中查看DIV.

While, trying to execute,that value does not print and I see an error as REFERENCE ERROR,id not defined andit is a blank screen where the DIV has to be viewed.

在HTML代码中,我给出了以下内容,

In the HTML code,I have given the following,

<div id="swipe_body">

        <div class="swiper-container swiper-threshold">
            <div class="swiper-wrapper">

            </div>
        </div>
    </div>

这可能吗?我犯了任何错误吗?

Is this possible or not? Have I committed any mistake?

在"@nnnnnn"建议之后,我更改并实现了以下代码:

AFTER "@nnnnnn" SUGGESTIONS I HAVE CHANGED AND IMPLEMENTED THE FOLLOWING CODE:

 var cnt1 = '<div id="container'+val+'"><span><img src="img/abc_'+val+'.png" style="float:left; " /></span><div id="abcd'+val+'"><span><h5>'+value[k]+'</h5></span></div></div>';
                 // console.log("div_id :"+id);
                  document.getElementById(divIdName).innerHTML=cnt1;
                  console.log("abcd values: "+value[k]);
                  ni.appendChild(newdiv);

但是,我遇到以下错误:

but,i am getting the following error:

05-14 17:24:25.382: I/Web Console(17882): JSCallback Error: TypeError: Cannot set property 'innerHTML' of null at file:///android_asset/www/cordova-2.1.0.js:3727

推荐答案

为了深入了解这一点,您需要检查:

In order to get to the bottom of this, you'd need to check:

  1. "divIdName"设置为什么
  2. 如果在执行脚本时实际存在"swiper-slide(divIdName)"

该错误基本上表明您正在尝试将innerHTML设置为空,这反过来意味着您要选择的元素不存在.

The error basically says that you're trying to set the innerHTML of nothing, which in turn means the element you're trying to select doesn't exist.

如果您console.log所有变量,我们可以为您提供更好的帮助.

If you console.log all your variables we can help you out better.

作为旁注:您应该真正清理代码并提升变量,调试意粉代码真的很难;-)

As a side note: you should really clean up your code and hoist your variables, it's really hard to debug spaghetti code ;-)

这篇关于我们可以使用变量在JAVASCRIPT中声明div吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:57
查看更多