【发布时间】:2016-04-27 11:38:48
【问题描述】:
我的问题是如何使用按钮在对话框上显示 Javascript 函数的结果?
你帮帮忙!
【问题讨论】:
标签: javascript function button
我的问题是如何使用按钮在对话框上显示 Javascript 函数的结果?
你帮帮忙!
【问题讨论】:
标签: javascript function button
Javascript
function yourfunction()
{
// code
return result;
}
HTML
<button onclick="alert(yourfunction());">Click</button>
演示
function myfunction()
{
return "It works";
}
<button onclick="alert(myfunction())">Clic</button>
【讨论】:
您可以尝试使用 jQuery UI 对话框:
function someFunction() {
return 42;
}
$(function() {
$('<div>' + someFunction() + '</div>').appendTo('body').dialog();
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
【讨论】:
像这样:
HTML
<button id="button">
Show results
</button>
JS
// The function whose results we want to print out
function foo() {
var result = "bar";
// we need to return the result
return result;
}
// get the button in the HTML
var button = document.getElementById("button");
// attach the click event on it
button.addEventListener("click", function() {
// after clicking the button, show the results
alert(foo());
});
【讨论】: