我一般来说是JavaScript和网络开发的新手,但我想做的是:创建不同的表单,并根据单击的按钮,运行特定的代码(目前仅显示内容)。>
这就是我所做的
<script>
function init() {
//this.onclick = showAlert;
var p1 = document.getElementById('btn-hello');
var p2 = document.getElementById ('btn-web');
var p3 = document.getElementById('btn-prive');
p1.onclick = showAlert('msghello.txt');
p2.onclick = showAlert('msgweb.txt');
p3.onclick = showAlert('msgprive.txt');
function showAlert(file) {
// Requiring fs module in which
// readFile function is defined.
$.get(file, function(data) { alert(data)}, 'text');
}
</script>
问题在于,无论我单击哪个按钮,它都会为每个按钮运行代码。 我也尝试使用此参数,但仍然无法正常工作。