javascript - 如何对在 javascript 中创建的 div 元素实现 javascript?

标签 javascript jquery html

我正在使用 this link 中的 JavaScript 。

我使用以下代码在 javascript 中创建了一个新的 div 元素

<script type="text/javascript" src="idangerous.swiper-1.9.1.min.js"></script>
<script type="text/javascript" src="swiper-demos.js"></script>
 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 superdiv = 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');
    newdiv.style.width = "25%";
newdiv.style.height = "30%";
    superdiv.appendChild(newdiv);
    var cnt1 = '<div id="container" class="container"><span><img src="img/cause_'+val+'.png" style="float:left;"></span><div id="clinicals'+val+'" class="clinical"><span ><h5>'+value[k]+'</h5></span></div></div>';
    console.log("check value"+cnt1);
    document.getElementById(divIdName).innerHTML=cnt1;
    console.log("clinical values: "+value[k]);
console.log("processsing parameter loop ");
var searchString = window.location.search.substring(1),i,val,params = searchString.split("&");
}

html代码

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

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

CSS代码:

.clinical       
{
    font-size:15px;text-justify:inter-word;margin-right:10px; margin-left:10px; margin-top:10px; margin-right:10px; margin-bottom:10px;
}
.container
{
 background:url(img/value_bg.png) no-repeat scroll 0 0 transparent; background-size:100% 100%; display:block; width:304px; height:250px;text-align:justify;
}
.container span
{
 width:auto; height:30%; display:block; overflow:hidden;float:left; 
}

输出是这样的

current out put

但我想得到这样的

before SWIPING

滑动时我应该得到这样的结果

enter image description here

请建议我解决这个问题的方法..

编辑:我在样式之后、javascript之前给出了以下代码。

</style>
    <link rel="stylesheet" type="text/css" href="css/reset.css" />
    <link rel="stylesheet" type="text/css" href="css/idangerous.swiper.css" />
    <link rel="stylesheet" type="text/css" href="css/swiper-demos.css" />
    <script type="text/javascript" charset="utf=8" src="cordova-2.1.0.js"></script> 
    <script type="text/javascript" charset="utf-8" src="SQLitePlugin.js"></script>
    <script type="text/javascript" charset="utf-8" src="jquery-1.8.2.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="main.js"></script>
    <script type="text/javascript" src="idangerous.swiper-1.9.1.min.js"></script>
    <script type="text/javascript" src="swiper-demos.js"></script>
    <script type="text/javascript" charset="utf-8">

编辑2:

 var cnt1 = '<div id="container" class="container"><span><img    src="img/cause_'+val+'.png" style="float:left;"></span><div id="clinicals'+val+'" class="clinical"><span ><h5>'+value[k]+'</h5></span></div></div>';
 console.log("check value"+cnt1);
 document.getElementById(divIdName).innerHTML=cnt1;
 document.querySelector('.swiper-container');

我添加了这个 document.querySelector('.swiper-container'); 以及

function onDeviceReady() 
{   var mySwiper = new Swiper('.swiper-container',{
mode:'horizontal',loop: true
 }); 

唯一的改进是只有第一张幻灯片存在,不能滑动。

最佳答案

您的代码中没有 Jquery,只有原生 JavaScript。

我建议您看看这个插件。 http://plugins.jquery.com/jcarousel/ 这是一个 jquery 轮播。我已经使用过它,它简单、文档齐全且灵活。 点击右上角“查看主页”,在开发者页面查看demo。

编辑这里有一个使用 JCarousel 的示例代码来帮助您:

<script type="text/javascript" src="/path/to/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/path/to/lib/jquery.jcarousel.min.js"></script>
<link rel="stylesheet" type="text/css" href="/path/to/skin/skin.css" />

<html>
    <body>
      <div id="swipe_body">
        <div class="swiper-container swiper-threshold" id="mycarousel">
          <div class="swiper-wrapper" id="swiper-wrapper">
              <div id="container1" class="container"><span><img...</span></div>';
              <div id="container2" class="container"><span><img...</span></div>';
              <div id="container3" class="container"><span><img...</span></div>';
              ... ...
          </div>
        </div>
      </div>
    </body>
</html>

<script>
   jQuery(document).ready(function() {
       jQuery('#mycarousel').jcarousel({
            // optional configuration goes here
       });
   });
</script>

正如你所看到的,只需要 2 行 jquery 就可以得到这个。 您可以在上面给出的网址下载jquery.jcarousel.min.js。只需使用服务器中的数据创建多个容器即可。请记住,避免为不同元素提供相同的 id

for (k=0;k<value.length;k++){
    ...
   var cnt1 = '<div id="container" cla
    ...

它们都将具有相同的id

关于javascript - 如何对在 javascript 中创建的 div 元素实现 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607929/

相关文章:

javascript - 根据字符串输入查找日期值

javascript - Chart.js 和右侧可用空间

javascript - React Stripe 拆分卡元素错误 : Maximum update depth exceeded

javascript - 警告框消失,无需单击 ASP.NET 中的“确定”按钮

jQuery 获取响应图像的高度

javascript - contenteditable div 中突出显示的文本 : Get the start and the end of the highlighted text

jquery - 无法在 jQuery 中切换背景图像

javascript - jquery - 单击事件不适用于表中动态创建的按钮

JavaScript Sprite 表用于普通和视网膜屏幕

html - IE8 的 CSS 定位 - 不支持表达式