我在让 jQuery 与 transcrypt 一起工作时遇到问题。这是我的代码。我想检索一个带负载的文件并将其放入一个变量中。函数 read_file 工作正常,并在 div 元素中显示 var。但是其他两个函数没有按预期工作:read_hidden_var 仅在第二次单击后才起作用,而 read_file_var 根本不起作用,产生错误提示:TypeError $.py_get is not a function。
这是代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__pragma__ ('alias', 'S', '$')
class TestSystem:
def read_file(self):
S("#demo").load("https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/README.md")
def read_hidden_var(self):
document.getElementById('hidden_text').style.display='block'
S("#hidden_text").load("https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/README.md")
console.log(S("#hidden_text").html())
self.file_content = S("#hidden_text").html()
S("#hidden_text").html("")
document.getElementById('hidden_text').style.display='none'
alert(self.file_content)
def read_file_var(self):
S.get("https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/README.md", self.get_filecontent)
def get_filecontent(self, response):
self.file_content = response
alert(self.file_content)
testSystem = TestSystem()
和html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="__javascript__/read_file2.js"; charset="UTF-8"></script>
<title>Read File</title>
</head>
<body>
<main>
<h1>Read a file!</h1>
<p id="p1" class="para1">Read a file!</p>
<button id="button1" onclick="$(document).ready(read_file2.testSystem.read_file)">Click for retrieving text file</button><br><br>
<button id="button1" onclick="$(document).ready(read_file2.testSystem.read_file_var)">Click for retrieving text file into var</button><br><br>
<button id="button1" onclick="$(document).ready(read_file2.testSystem.read_hidden_var)">Click for retrieving text file into hidden paragraph</button><br><br>
<p id="demo"></p>
<p id="hidden_text"></p>
</main>
</body>
</html>