您的位置:首页 > 其它

当页面上任意一个链接被点击的时候,alert出这个链接在页面上的顺序号

2016-01-10 00:21 555 查看
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<style text="text/css">
body{
background-color:pink;
font-size:24px;
text-align:center;
}
p{
line-height:0.86;
}
</style>
<script type="text/javascript">
function clickme(x){
var dom=document.getElementsByTagName("p");
//该点击的节点
for(var i=0;i<dom.length;i++){
if(x==dom[i]){
//alert(x.id);
alert(i+1);
break;
}
}
}
function add_jd(){
var id_str=document.getElementsByTagName("p").length+1;
var para=childpoint=document.createElement("p");
para.id="aa".concat(id_str);
para.innerHTML=para.id;
para.onclick=function (){
var dom=document.getElementsByTagName("p");
for(var i=0;i<dom.length;i++){
if(this==dom[i]){
//alert(this.id);
alert(i+1);
break;
}
}

}
document.getElementsByTagName("body")[0].appendChild(para);

}
</script>
</head>
<body>
<input onclick="add_jd()" type="button" value="点击我吧"></input>
<p id="first" onclick="clickme(this)">first 1</p>
<p id="second" onclick="clickme(this)">second 2</p>
<p id="third" onclick="clickme(this)">third 3</p>
<p id="fourth" onclick="clickme(this)">fourth 4</p>
<p id="fifth" onclick="clickme(this)">fifth 5</p>
<p id="sixth" onclick="clickme(this)">sixth 6</p>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: