您的位置:首页 > Web前端 > JQuery

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

2017-06-14 23:22 483 查看

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

<foreach name="artData" item="vo">
<div class="article">
<h2><a href="blog.html">{$vo.title}</a></h2>
<p>by<a href="#">xxx</a>  |  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>  |  <a href="#" class="com">评论 ({$vo.comm})</a>  |  <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);

});

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery thinkphp