jq each的用法

smith_chan 2011-04-03 05:55:24
有这样一段html
<div id = "testDiv">
<button id="button1">button1</button>
<button id="button2">button2</button>
<button id="button3">button3</button>
</div>
<div id="divMsg"></div>
<button id = "testBtn">内容</button>

我想在 $($("#testDiv").each).bind("click", function(event) {
$("#divMsg").html(
遍历时能够获取所有的子标签对象,然后分别操作 );
});
...全文
922 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 社区高级成员 T9 2011-04-13
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<style type="text/css">
.even{background-color:gray;}
.odd{background-color:white;}
</style>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
<script type="text/javascript">
$(function() {
$("#testDiv *").each(function(i) {
$(this).click(function() {
alert($(this).attr("id"));
});
});
});
</script>
</head>
<body>
<form >
<div id = "testDiv">
<button id="button1">button1</button>
<button id="button2">button2</button>
<button id="button3">button3</button>
</div>
<div id="divMsg"></div>
<button id = "testBtn">内容</button>
<p>
我想在 $($("#testDiv").each).bind("click", function(event) {
$("#divMsg").html(
遍历时能够获取所有的子标签对象,然后分别操作 );
});
</p>
</form>
</body>
</html>
zhouyuehui 2011-04-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 kingdom_0 的回复:]

$("#testDiv").each(function() {
$(this).bind('click', function(event){
......
});
[/Quote]
正解
jhrxxx 2011-04-12
  • 打赏
  • 举报
回复
$("#testDiv button").each(function(i){
$(this).bind("click",function(event){

});
})
sjkof 2011-04-12
  • 打赏
  • 举报
回复
直接用this (html element)
如果需要jquery element就用$()包装一下
kingdom_0 2011-04-11
  • 打赏
  • 举报
回复
$("#testDiv").each(function() {
$(this).bind('click', function(event){
......
});
lazyboy_wu 2011-04-04
  • 打赏
  • 举报
回复


我想在 $("#testDiv").bind("click", function(event) {
$("#divMsg").html(
// 绑定click以后,触发点击事件以后,这里this就是元素对象了
this.id
);
});


llyy112233 2011-04-03
  • 打赏
  • 举报
回复

<div id = "testDiv">
<button id="button1">button1</button>
<button id="button2">button2</button>
<button id="button3">button3</button>
</div>
<div id="divMsg"></div>
<button id = "testBtn">内容</button>

$("#testDiv").each(function() {
$(this).bind('click', function(event){
......
});
);

打字员 2011-04-03
  • 打赏
  • 举报
回复
自己看API吧

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧