js获取当前点击的<li>的值
问题是这样的:我用javacript函数给一个<ul></ul>里面动态添加了几个<li>,当我到浏览器的时候,如何通过点击<li>,获取这个点击的<Li>的值。<li>...
问题是这样的:我用javacript函数给一个<ul></ul>里面动态添加了几个<li>,当我到浏览器的时候,如何通过点击<li>,获取这个点击的<Li>的值。<li>是在运行的时候才出来的.麻烦给出简单的例子,谢谢
展开
1个回答
展开全部
<!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>
<style type="text/css">
.ul1{
height:50px;
list-style-type:none;
margin-top:2px;
margin-right:0px;
margin-bottom:0px;
margin-left:0px;
}
.li1{
width:46px;
float:left;
margin-left:2px;
border-top:1px solid #000000;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
border-left:1px solid #000000;
height:46px;
list-style-type:none;
text-align:center;
line-height:46px;
}
</style>
<script>
function aa(num){
var bombs = new Array(num);
var flag = 0;
while( flag < num ){
bomb = Math.round(Math.random()*(num*num-1));
var f = true;
for(var i = 0 ; i <=flag ; i++){
if(bomb == bombs[i]){
f = false;
break;
}
}
if(f){
bombs[flag++] = bomb;
}
}
return bombs;
}
function createDiv(num,bombs){
var pDiv = document.getElementById("pdiv");
for(i = 0 ; i < num ; i++){
var flag = false;
var newUl = document.createElement("ul");
newUl.className = "ul1";
for(j = 0 ; j < num ; j++){
var newLi = document.createElement("li");
newLi.className = "li1";
newLi.id = "li" + (i*num + j);
flag = 0;
for(k = 0 ; k < num ; k++){
if(bombs[k] == (i*num + j)){
flag = true;
break;
}
}
if(flag){
var newtext = document.createTextNode(-1);
}else{
var newtext = document.createTextNode(0);
}
newLi.appendChild(newtext);
newUl.appendChild(newLi);
}
pDiv.appendChild(newUl);
}
}
function creatBox(num){
var bombs = aa(num);
createDiv(num,bombs);
return bombs;
}
function play(num){
var bombs = creatBox(num);
var temps = new Array(num*num -1);
for(i = 0 ; i < num*num; i ++){
temps[i] = 0;
}
for( j = 0 ;j < num; j++){
temps[bombs[j]] = -1;
}
lis = document.getElementsByTagName("li");
for(i = 0 ; i < lis.length ; i++){
lis[i].onclick=function ss(){
liid = this.id;
num = parseInt(liid.substring(2));
if(temps[num] == -1){
alert("你踩到雷了");
}else{
alert(temps[num]);
}
}
lis[i].oncontextmenu=function tt(){
liid = this.id;
num = parseInt(liid.substring(2));
if(temps[num] == -1){
alert("你排到雷了");
}else{
alert(temps[num]);
}
return false;
}
}
}
function myplay(){
var pDiv = document.getElementById("pdiv");
pDiv.innerHTML = "";
num = parseInt(document.getElementById("nd").value);
play(num);
}
</script>
<select name="nd" onchange="myplay()" id="nd">
<option value="4">简单</option>
<option value="10">中等</option>
<option value="20">难</option>
</select>
</head>
<body>
<div id="pDiv">
</div>
<script language="javascript">
play(6);
</script>
</body>
</html>
最近学dom写的一个小代码,本来是写扫雷的,也没写完,你看看,我现在就是点击li,显示li里的内容,运行一下就知道了
原理还是创建li的时候给它加上id
var newLi = document.createElement("li");
newLi.className = "li1";
newLi.id = "li" + (i*num + j);
然后取的时候根据id取就可以了
lis = document.getElementsByTagName("li");
for(i = 0 ; i < lis.length ; i++){
lis[i].onclick=function ss(){
liid = this.id;
num = parseInt(liid.substring(2));
if(temps[num] == -1){
alert("你踩到雷了");
}else{
alert(temps[num]);
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
.ul1{
height:50px;
list-style-type:none;
margin-top:2px;
margin-right:0px;
margin-bottom:0px;
margin-left:0px;
}
.li1{
width:46px;
float:left;
margin-left:2px;
border-top:1px solid #000000;
border-right:1px solid #000000;
border-bottom:1px solid #000000;
border-left:1px solid #000000;
height:46px;
list-style-type:none;
text-align:center;
line-height:46px;
}
</style>
<script>
function aa(num){
var bombs = new Array(num);
var flag = 0;
while( flag < num ){
bomb = Math.round(Math.random()*(num*num-1));
var f = true;
for(var i = 0 ; i <=flag ; i++){
if(bomb == bombs[i]){
f = false;
break;
}
}
if(f){
bombs[flag++] = bomb;
}
}
return bombs;
}
function createDiv(num,bombs){
var pDiv = document.getElementById("pdiv");
for(i = 0 ; i < num ; i++){
var flag = false;
var newUl = document.createElement("ul");
newUl.className = "ul1";
for(j = 0 ; j < num ; j++){
var newLi = document.createElement("li");
newLi.className = "li1";
newLi.id = "li" + (i*num + j);
flag = 0;
for(k = 0 ; k < num ; k++){
if(bombs[k] == (i*num + j)){
flag = true;
break;
}
}
if(flag){
var newtext = document.createTextNode(-1);
}else{
var newtext = document.createTextNode(0);
}
newLi.appendChild(newtext);
newUl.appendChild(newLi);
}
pDiv.appendChild(newUl);
}
}
function creatBox(num){
var bombs = aa(num);
createDiv(num,bombs);
return bombs;
}
function play(num){
var bombs = creatBox(num);
var temps = new Array(num*num -1);
for(i = 0 ; i < num*num; i ++){
temps[i] = 0;
}
for( j = 0 ;j < num; j++){
temps[bombs[j]] = -1;
}
lis = document.getElementsByTagName("li");
for(i = 0 ; i < lis.length ; i++){
lis[i].onclick=function ss(){
liid = this.id;
num = parseInt(liid.substring(2));
if(temps[num] == -1){
alert("你踩到雷了");
}else{
alert(temps[num]);
}
}
lis[i].oncontextmenu=function tt(){
liid = this.id;
num = parseInt(liid.substring(2));
if(temps[num] == -1){
alert("你排到雷了");
}else{
alert(temps[num]);
}
return false;
}
}
}
function myplay(){
var pDiv = document.getElementById("pdiv");
pDiv.innerHTML = "";
num = parseInt(document.getElementById("nd").value);
play(num);
}
</script>
<select name="nd" onchange="myplay()" id="nd">
<option value="4">简单</option>
<option value="10">中等</option>
<option value="20">难</option>
</select>
</head>
<body>
<div id="pDiv">
</div>
<script language="javascript">
play(6);
</script>
</body>
</html>
最近学dom写的一个小代码,本来是写扫雷的,也没写完,你看看,我现在就是点击li,显示li里的内容,运行一下就知道了
原理还是创建li的时候给它加上id
var newLi = document.createElement("li");
newLi.className = "li1";
newLi.id = "li" + (i*num + j);
然后取的时候根据id取就可以了
lis = document.getElementsByTagName("li");
for(i = 0 ; i < lis.length ; i++){
lis[i].onclick=function ss(){
liid = this.id;
num = parseInt(liid.substring(2));
if(temps[num] == -1){
alert("你踩到雷了");
}else{
alert(temps[num]);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询