【问题标题】:ipython javascript not updating textipython javascript不更新文本
【发布时间】:2014-11-08 06:33:46
【问题描述】:

这似乎可以作为在浏览器中打开的纯 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>

【问题讨论】:

  • 在 document.ready() 中包装 jquery 代码。
  • @Kartikeya 这应该是不必要的;代码遵循它影响的 DOM 元素。 (也许 ipython 很奇怪?)
  • jQuery 1.3 也很老了。
  • 是的,同意 Pointy 1.3 版本太旧,只需升级 jquery 版本。
  • 我只在纯(非 IPython)版本中使用旧 jquery,并且 确实 工作。所以这不应该是问题。

标签: javascript jquery ipython


【解决方案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)

【讨论】:

    猜你喜欢
    • 2019-04-05
    • 1970-01-01
    • 2013-05-27
    • 2010-10-23
    • 2015-03-26
    • 2013-03-25
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多