单击按钮以显示索引警报

时间:2017-06-08 02:29:30

标签: javascript html

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompress {
    public static void compress(String dirPath) {
        final Path sourceDir = Paths.get(dirPath);
        String zipFileName = dirPath.concat(".zip");
        try {
            final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                    try {
                        Path targetFile = sourceDir.relativize(file);
                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
                        byte[] bytes = Files.readAllBytes(file);
                        outputStream.write(bytes, 0, bytes.length);
                        outputStream.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

页面上有20个按钮排成一排。如何单击每个按钮以提醒其索引。 Ex点击button1将提示1

3 个答案:

答案 0 :(得分:2)

&#13;
&#13;
<script>
  for(var i = 0; i < 20; i++) {
     document.write("<button onclick='alert("+(i + 1)+")'>Button " + (i + 1) +"</button> <br/> <br/>");
  }
</script>
&#13;
&#13;
&#13;

您好,上面的简单方法,它适合我。请运行代码段。

问候 潮

答案 1 :(得分:1)

ty<pe="button"应该是type="button"的拼写错误。除此之外,你可以直接为内联函数编写警报,因为你的函数myFunction实际上没有做任何有效的工作。

var index = 21;
var Buttons = $('#Buttons');

for (var i = 1; i <index; i++) {
  Buttons.append('<input type="button" onclick="alert('+i+')" id="button" ' + i + '" value="Button' + i + '"/>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="Buttons"></div>

答案 2 :(得分:0)

enter code here

var totalButton = 20;
var buttonsDiv = document.getElementById('buttons');

for (var i = 1; i <totalButton; i++) {
   var button = document.createElement("input");
   button.type = "button";
   button.value= "Button "+i;
   button.id = i;
   button.onclick = function(event){
    alert(event.target.id);
    }
   buttonsDiv.append(button);
}
<div id="buttons"></div>