我希望在按下按钮时发生这种情况,而不是在我打开/刷新网站时。有人可以帮助我吗? :):
<script type="text/javascript">
function getRate(from, to) {
var script = document.createElement('script');
script.setAttribute('src', "http://query.yahooapis.com/v1/public/yql?q=select%20rate%2Cname%20from%20csv%20where%20url%3D'http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes%3Fs%3D"+from+to+"%253DX%26f%3Dl1n'%20and%20columns%3D'rate%2Cname'&format=json&callback=parseExchangeRate");
document.body.appendChild(script);
}
function parseExchangeRate(data) {
var name = data.query.results.row.name;
var rate = parseFloat(data.query.results.row.rate, 10);
alert("Exchange rate " + name + " is " + rate);
}
getRate("USD", "NOK");
</script>
答案 0 :(得分:0)
您可以使用按钮的onclick属性:
<button onclick="getRate('USD', 'NOK')">clickme</button>
并从脚本末尾删除对getRate()
的调用,以便在页面加载时不会调用它。