thinkphp循环数据到页面时,jquery$(this)动态获取指定元素的数据

来源:互联网 发布:网络运维 英文缩写 编辑:程序博客网 时间:2024/06/07 02:10

用tp框架在前端循输出数据时遇到的情况

 <foreach name="artData" item="vo">        <div class="article">          <h2><a href="blog.html">{$vo.title}</a></h2>          <p>by<a href="#">xxx</a> &nbsp;|&nbsp; xxxx<a href="#">{$vo.nick}</a>          <a href="blog.html"><img src="__PUBLIC__/home/images/img1.jpg" width="613" height="154" alt="image" /></a>          <p>{$vo.content}<a href="http://www.imooc.com/">xxx</a>.</p>          <p class="spec"><a href="#" class="rm">Read more</a> &nbsp;|&nbsp; <a href="#" class="com">评论 ({$vo.comm})</a> &nbsp;|&nbsp; <span id="pubtime" class="date">{$vo.pubtime}</span></p>        </div>        </foreach>

当为循环出来的每一个.artucle绑定一个click事件时候,$(“”)内填什么由为重要当填id class 元素标签。。。都会遇到一个问题就是页面的加载时,用的永远都是第一次循环出来的id class 元素标签。所有$(this)上场,动态绑定,谁点击,谁绑定,并且用$(this).find().text()能够有效的获取内容

 
$(document).ready(function(){
$(‘.article’).click(function(){
var pubtime= $(this).find(“#pubtime”).text();
alert(pubtime);
});
});

阅读全文
0 0
原创粉丝点击