js向页面动态添加无序列表li,实现如下效果

js向页面动态添加无序列表li,实现如下效果,第1张

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gb2312" />

<title>文哥讨厌IE</title>

<script src="jquery.js"></script>

</head>

<body>

    <input type="button" value="Add items" class="Add_items" />

        <ul class="conT">

        <li>Item1</li>

        <li>Item2</li>

        <li>Item3</li>

    </ul>

    <script>

        $(document).ready(function(){

          $(".Add_items").click(function(){

              var num=window.prompt("请输入n")

              var index =parseInt(num)+4

             for(var i=4i<indexi++){

                 $("li:last").after("<li>Item"+i+"</li>")

             }

          })

        })

    </script>

</body>

</html>

采用Jquery实现的列表数据动态更新效果,更新的数据可以是ajax请求的数据。

CSS:

.main

{

width:

100%

margin-top:

100px

text-align:

center

font-size:

12.5px

}

th,

td

{

border:

1px

solid

#ccc

line-height:

40px

padding-left:

5px

}

.item:hover

{

background-color:

#efefef

}

.item:nth-child(2n)

{

background-color:

#efefef

}

.ListView

{

width:

600px

overflow:

hidden

margin:

0

auto

padding:

10px

height:372px

border:

1px

solid

#dddddd

}

.ListView

.c

{

width:

1200px

margin:

0

auto

border-collapse:

collapse

}

.Item

{

border-bottom:

1px

dashed

#dddddd

padding:

10px

0

10px

0

overflow:

hidden

margin-left:600px

}

.Item

span

{

float:

left

text-align:

left

}

.Item

span:first-child

{

color:

#6AA8E8

}

.Item

span:last-child

{

text-align:

center

}

HTML

<div

class="main">

<div

class="ListView">

<div

class="c">

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

<div

class="Item">

<span>test</span>

<span>男/0</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>

</div>

</div>

</div>

<p

style="text-align:center"><a

href="javascript:void(0)"

onClick="ListView.Update()">刷新数据</a></p>

JS

<script

type="text/javascript"

src="/js/jquery-1.8.0.min.js"></script>

<script

type="text/javascript">

$(function(){

ListView.Init()

})

var

ListView={

Init:function(){

$(".Item

span").css("width",$(".ListView").width()/4+"px")

for(var

i=0i<$(".Item").lengthi++){

var

target=$(".Item")[i]

$(target).animate({marginLeft:"0px"},300+i*100)

}

},

Update:function(){

$(".ListView

.c

.Item").remove()

for(var

i=0i<10i++){

var

newItem=$("<div

class=\"Item\">

<span>test</span>

<span>男/"+i+"</span>

<span>四川省,成都市,锦江区</span>

<span>详细说明</span>

</div>")

$(newItem).find("span").css("width",$(".ListView").width()/4+"px")

$(".ListView

.c").append(newItem)

$(newItem).animate({marginLeft:"0px"},300+i*100)

}

}

}

</script>

附上演示效果

http://demo.jb51.net/js/2015/jquery-dtlb

效果是不是非常棒呢,接下来我们再来看看瀑布流的实现思路和js控制动态加载的代码

下面的代码主要是控制滚动条下拉时的加载事件的

在下面代码说明出,写上你的 *** 作即可,无论是加载图片还是加载记录数据

都可以

别忘了引用jquery类库

$(window).scroll(function

()

{

var

scrollTop

=

$(this).scrollTop()

var

scrollHeight

=

$(document).height()

var

windowHeight

=

$(this).height()

if

(scrollTop

+

windowHeight

==

scrollHeight)

{

//此处是滚动条到底部时候触发的事件,在这里写要加载的数据,或者是拉动滚动条的 *** 作

//var

page

=

Number($("#redgiftNextPage").attr('currentpage'))

+

1

//redgiftList(page)

//$("#redgiftNextPage").attr('currentpage',

page

+

1)

}

})

解析:

判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop、clientHeight、scrollHeight。

scrollTop为滚动条在Y轴上的滚动距离。

clientHeight为内容可视区域的高度。

scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。

从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop

+

clientHeight

==

scrollHeight。(兼容不同的浏览器)。

1、在页面div中事先创建一个空白表格,可以根据需求而定。

2、表格创建好后,我们就可以写动态生成表格的关键代码了。我们写一个js方法供触发使用。

3、在<tb>标签中我们添加了<input>标签,主要是用来提供用户输入参数, 而全局变量num,主要是用来区分每一个添加的参数的id的唯一性而存在的。

4、获得表格中的数据。

5、拿到表格中的数据后,我们根据它值的形式把它转换成json格式的数据。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11646214.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存