我有问题可以让jQuery与TransCrypt合作。这是我的代码。我想通过负载检索文件,然后将其放入变量中。函数read_file工作正常,并在DIV元素中显示VAR。但是其他两个函数无法按预期工作:read_hidden_var仅在单击第二次之后起作用,并且read_file_var根本不起作用,出现错误,说:typeError $ .py_get不是函数。
这是代码:
#!/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>
" py_get不是函数"报告是由混叠引起的。 get
一词在python中具有特殊的含义,因此默认情况下是与 py_get
的别名,在此上下文中不存在。您可以unalias get
或使用js_get
,因为这是与get
相反的。所有别名都有类似的选项,如:
http://www.transcrypt.org/docs/html/special_facilities.html#pragma-alias
在下面的修改代码中,我使用了js_get
,因为它的影响最小。(要查找修改,搜索!!!
(
双击问题很难解决。我已经用纯JavaScript代替了该代码,并且问题仍然存在。程序逻辑。但是我目前无法确定它。
#!/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):
# !!! Literally inserted JavaScript does not solve the second problem,
# so probably this isn't specific to Transcrypt
__pragma__ ('js', '{}', '''
console.log ("BEGIN read_hidden_var");
document.getElementById("hidden_text").style.display="block";
$("#hidden_text").load("https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/README.md");
console.log($("#hidden_text").html());
self.file_content = $("#hidden_text").html();
$("#hidden_text").html("");
document.getElementById("hidden_text").style.display="none";
alert(self.file_content);
console.log ("END read_hidden_var");
''')
def read_file_var(self):
# !!! Using js_get solves the first problem
S.js_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()