在html表中弹出Javascript

问题描述 投票:1回答:2

我有一个10 x 10的数据表,使用html标签创建。如果可以为每个单元格创建onClick函数?我的意思是,如果我点击一个单元格,那么它会在n警告窗口中显示它的值吗?如果是,那怎么样?

javascript html html-table popup
2个回答
4
投票

普通JS - 假设<table id="table1">

window.onload=function() {
  var cells = document.getElementById('table1').getElementsByTagName('td');
  for (var i=0, n=cells.length;i<n;i++) {
    cells[i].onclick=function() { alert(this.innerHTML) }
  }
}

0
投票

这里可以找到一个很好的例子

HTML CODE: 

<div id='page-wrap'>

Your content goes here.

<a id='show' href='#'>show overlay</a>


JavaScript & JQuery:

$(document).ready(function() {

$("#show").click(function() {
    showPopup();
});

$("#popup").click(function() {

    $(this).hide();
    $("#mask").hide();

});

});

function showPopup(){

   // show pop-up

$("#mask").fadeTo(500, 0.25);

// show the popup
$("#popup").show();
}



 ---------------------------------------


 CSS CODE: 


* { margin: 0, padding 0}

#mask{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
display: none;
z-index: 10000;

}

#popup
{
width: 200px;
height: 200px;
margin: 10px auto; 
border: 1px solid #333;
background-color: #ffffdd;
cursor: pointer;
padding: 10px;
position: relative;
z-index: 10001;
display: none;
}


  [![enter image description here][1]][1]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.