这是我的 HTML:
<textarea cols="5" disabled id="textareRSAKeypair">
@Model["keypair"]
</textarea>
<a href="#" class="btn btn-primary" id="downloadKeypair">DownLoad Key</a>
这是我的 jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$("a#downloadKeypair").click(function () {
var now = new Date().toString();
var filename = 'RSAKeyPair_' + now + ".txt";
$("a#downloadKeypair").attr('Download', filename);
this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent($('#textareRSAKeypair').text());
});
</script>
我的 jQuery 代码在页面上不起作用,但是当我将它复制并粘贴到控制台 (google chrome) 时,它运行良好。我不知道为什么。我尝试添加 document.load
方法,但它仍然不起作用。
最佳答案
尝试就绪事件:
$( document ).ready(function() {
$("a#downloadKeypair").click(function () {
var now = new Date().toString();
var filename = 'RSAKeyPair_' + now + ".txt";
$("a#downloadKeypair").attr('Download', filename);
this.href = "data:text/plain;charset=UTF-8," + encodeURIComponent($('#textareRSAKeypair').text());
});
});
关于javascript - jQuery 不起作用,除非我复制并粘贴到谷歌浏览器控制台,它运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22874040/