在Javascrip的alert框中打印python变量

2024-03-29 15:46:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法将python变量打印到Javascript警报框中。有什么想法吗? 我试着用我的代码来工作,但似乎没什么不同。在

#!/bin/python
variable="hi"
print "Content-type: text/html"
print
print '''<html>
<head><title>Python Script</title></head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<body>
'''
print "<script>"
print "alert('Hello! I am an alert box!!  %s ');"
print "</script>"% (variable)
'''
</body>
</html>'''

Tags: textsrccomhttphtmltypejsscript
2条回答
print '''<script>
alert('Hello! I am an alert box!!  %s ');
</script>''' % (variable)
print "alert('Hello! I am an alert box!!  %s ');"% (variable)
print "</script>"

你必须用占位符替换同一个字符串。。。在

相关问题 更多 >