下面是完整的代码,你要懒的话,就直接copy吧
- <!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></title>
- <script src="js/jquery-1.7.1.min.js" type="text/Javascript"></script>
- <script type="text/javascript">
- $.log = function(msg) {
- console.log(msg);
- }
- $(
- function() {
- [{ number: 1, bgcolor: "#C71585" }, { number: 2, bgcolor: "#800080" }, { number: 3, bgcolor: "#B8860B" },
- { number: 4, bgcolor: "rgb(0,0,128)" }, { number: 5, bgcolor: "rgb(30,144,255)" },
- { number: 6, bgcolor: "rgb(255,165,0)" },
- { number: 7, bgcolor: "hsl(0,75%,50%)" }, { number: 8, bgcolor: "hsl(30,50%,50%)" },
- { number: 9, bgcolor: "hsl(120,75%,38%)"}].forEach(
- function(key, index) {
- //$.log(key);
- var li = $("<li>").html(key.number).css("backgroundColor", key.bgcolor).attr("draggable", "true");
- //$.log(li);
- li[0].addEventListener("dragstart", function(e) {
- e.dataTransfer.effectAllowed = "copyMove";
- e.dataTransfer.setData("text/plain", this.innerHTML);
- $.log(this.innerHTML);
- [ ].forEach.call(document.querySelectorAll("#player .default"),
- function(item) {
- //$.log(item);
- item.classList.remove("default");
- item.classList.add("ation");
- });
- }, false);
- li[0].addEventListener("dragend", function() {
- [ ].forEach.call(document.querySelectorAll("#player .ation"),
- function(item) {
- item.classList.remove("ation");
- item.classList.add("default");
- });
- }, false);
- $("#numberBox").append(li);
- }
- );
- }
- );
- $(
- function() {
- "500000300090500400004000700051037289302080604008052137035000900609000823080023006".split("").forEach(
- function(item, index) {
- $.log(item);
- var li = $("<li>")
- if (item != "0") {
- li.addClass("fix");
- li[0].innerHTML = item;
- }
- else {
- li[0].classList.add("default");
- li[0].addEventListener("dragenter",
- function(e) {
- $.log(e);
- }, false);
- li[0].addEventListener("dragover",
- function(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- $.log(e);
- return false;
- }, false);
- li[0].addEventListener("dragleave",
- function(e) {
- }, false);
- li[0].addEventListener("drop",
- function(e) {
- if (e.preventDefault) {
- e.preventDefault(); //不要执行与事件关联的默认动作
- }
- if (e.stopPropagation) {
- e.stopPropagation(); //停止事件的传播
- }
- var sendData = e.dataTransfer.getData("text/plain");
- //获得#player>li矩阵数组
- var matrix = Array.prototype.slice.call(document.querySelectorAll("#player>li"));
- var currIndex = matrix.indexOf(this); //获得当前元素的位置
- var rowIndex = currIndex - currIndex % 9; //行开始的位置
- var colIndex = currIndex % 9//列开始的位置
- for (var i = rowIndex; i < rowIndex + 9; i++) {
- if (i != currIndex && matrix[i].innerHTML == sendData) {
- alert("对不起行上有数据重复,请小心哦!亲");
- return;
- }
- }
- for (var i = colIndex; i < 81; i = i + 9) {
- if (i != currIndex && matrix[i].innerHTML == sendData) {
- alert("对不起列上有数据重复,请小心哦!亲");
- return;
- }
- }
- this.innerHTML = sendData;
- }, false);
- }
- $("#player").append(li);
- }
- );
- }
- );
- </script>
- <style type="text/css">
- #numberBox > li
- {
- width: 30px;
- height: 25px;
- text-align: center;
- font-size: 20px;
- padding-top: 5px;
- float: left;
- color: White;
- }
- #player .default
- {
- float: left;
- width: 48px;
- height: 33px;
- border: solid 1px rgb(0,0,0);
- font-size: 20px;
- padding-top: 15px;
- text-align: center;
- background-color: #B8860B;
- }
- #player .fix
- {
- float: left;
- width: 48px;
- height: 33px;
- border: solid 1px rgb(0,0,0);
- font-size: 20px;
- padding-top: 15px;
- text-align: center;
- background-color: #FFFABC;
- }
- #player .ation
- {
- float: left;
- width: 48px;
- height: 33px;
- border: solid 1px rgb(0,0,0);
- font-size: 20px;
- padding-top: 15px;
- text-align: center;
- background-color: #FFA07A;
- }
- </style>
- </head>
- <body>
- <ol id="numberBox" style="list-style-type: none; width: 90px; height: 90px;">
- </ol>
- <ol id="player" style="list-style-type: none; width: 450px; height: 450px;">
- </ol>
- </body>
