怎么知道点了哪个? id不能一样啊 加个自定义属性表示第几个div 点击后获取这个属性 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好吧,如果不是id,而且class,如果必须要这样做的话,该怎么实现? 内部再加一个节点,获取节点的内容XXX 我这边没环境试验,你获取它的style--backgroundcolor,它们颜色不是不一样么,试试吧 <!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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script src="jquery-1.8.2.min.js"></script><script>$(function(){ $("div").click(function(e) { alert("你点了第"+($(this).closest('div').index()+1)+"个div"); }); })</script></head><body><div id="t" style="width:100px; height:100px; background-color:#FF0000;">1</div><div id="t" style="width:100px; height:100px; background-color:#1CDF05;">2</div><div id="t" style="width:100px; height:100px; background-color:#FF0000;">3</div><div id="t" style="width:100px; height:100px; background-color:#1CDF05;">4</div><div id="t" style="width:100px; height:100px; background-color:#FF0000;">5</div><div id="t" style="width:100px; height:100px; background-color:#1CDF05;">6</div></body></html> jQuery自己引入一个在补充说一句id不能重复定义 <div id="t" style="width:100px; height:100px; background-color:#FF0000;" onclick="alert('这是第1个DIV')"></div><div id="t" style="width:100px; height:100px; background-color:#1CDF05;" onclick="alert('这是第2个DIV')"></div> var divs = $("[id=t]");divs.each(function(obj){alert($(obj).html());}); 关于jquery ui 的dialog使用问题 css , javascript 网页系统导入的问题! 悬浮按钮 关于页面自动添加控件的问题,急 top到底怎么用呀 送分题:一个标准的菜鸟问题 请问我常常对诸如: document.body.innerHTML 以及 连锁反应 如何预览在文本框中编辑的HTML文件???在线等侍!!! 寻求help 请教高手一些问题! $(document).on("mouseover",".close",function()什么意思?怎么调用? 关于js语法问题,大神请进!
好吧,如果不是id,而且class,如果必须要这样做的话,该怎么实现?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.8.2.min.js"></script>
<script>
$(function(){
$("div").click(function(e) {
alert("你点了第"+($(this).closest('div').index()+1)+"个div");
});
})
</script>
</head>
<body>
<div id="t" style="width:100px; height:100px; background-color:#FF0000;">1</div>
<div id="t" style="width:100px; height:100px; background-color:#1CDF05;">2</div>
<div id="t" style="width:100px; height:100px; background-color:#FF0000;">3</div>
<div id="t" style="width:100px; height:100px; background-color:#1CDF05;">4</div>
<div id="t" style="width:100px; height:100px; background-color:#FF0000;">5</div>
<div id="t" style="width:100px; height:100px; background-color:#1CDF05;">6</div>
</body>
</html>
<div id="t" style="width:100px; height:100px; background-color:#1CDF05;" onclick="alert('这是第2个DIV')"></div>
var divs = $("[id=t]");
divs.each(function(obj){
alert($(obj).html());
});