ipython javascript没有更新文本

时间:2014-09-14 17:38:15

标签: javascript jquery ipython

这似乎是在浏览器中打开的普通html / javascript文件,但是当我把它放在IPython笔记本中时,只有按钮文本正在更新(段落文本不会更新)。可能导致这种情况的原因是什么?

IPython笔记本电脑代码:     来自IPython.display导入HTML,Javascript,显示

display(HTML("""
<p id="demo">A Paragraph.</p>
<button id="something">Try it</button>
"""))
js = """
$("#something").click(function() {
$('#demo').text("Changed text");
$('#something').text('Change button');
});
"""
js = Javascript(js)
display(js)

普通的html / javascript:

<html>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<p id="demo">A Paragraph.</p>
<button id="something">Try it</button>
<script>
$("#something").click(function() {
$('#demo').text("Changed text");
$('#something').text('Change button');
});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

Kartikeya的建议有效:

from IPython.display import HTML, Javascript, display

display(HTML("""
<p id="demo">A Paragraph.</p>
<button id="something">Try it</button>
"""))
js = """
$(document).ready(
function() {
    $("#something").click(function() {
        $('#demo').text("Changed text");
        $('#something').text('Change button');
        });
    });
"""
js = Javascript(js)
display(js)