所以我搜索了很多,并想出了如何将 PHP 变量传递给 Javascript 函数,但是当我在我的代码中实现它时,而不是在 alert() 中获取变量;我得到了窗口<?php echo $a ?>
,<?php echo $c ?>
希望这是我忽略的小东西,但我不知道为什么这不起作用,因为我从论坛上的响应中逐行复制它并且用户说它有效,我有一个朋友说它有效,但是当我自己运行代码时(第二个 [code][/code]),它返回相同的错误。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<?php
$a = 'Fatber Christmas';
$b = '27 Sunshine Street /n America';
$c = nl2br($b);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Kenneth So Photography</title>
<link rel="stylesheet" href="kennethsostylesheet.css" type="text/css">
<link rel="shortcut icon" href="Photos/favicon.png" type="image/png">
<script type="text/javascript">
function test(a,b) {
alert(a);
alert(b);
}
</script>
<script type="text/javascript" src="picScript.js"></script>
</head>
<body>
<a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a>
<div id="headerBar">
<div id="logo">
<a href="javascript:void(0)" ><img src="Photos/FinalizedLogo.png" alt="logo" height="100px" width="400px" onclick="setNum(1)"></a>
</div>
<div id="titleBar">
<p>
<a href="javascript:void(0)" onclick="setNum(1)">Home</a> |
<a href="javascript:void(0)" onclick="setNum(2)">Automotive</a> |
<a href="javascript:void(0)" onclick="setNum(3)">Nightlife</a> |
<a href="/about.html">About Me</a> |
<a href="/contact.html">Contact</a>
</p>
</div>
</div>
<div id="imageBox">
<div id="imageView">
</div>
<div id="imagePreview">
</div>
</div>
</body>
</html>
产生相同显示的独立代码
<?php
$a = 'Fatber Christmas';
$b = '27 Sunshine Street /n America';
$c = nl2br($b);
?>
<html>
<head>
<script type="text/javascript">
function test(a,b) {
alert(a);
alert(b);
}
</script>
</head>
<body>
<a onClick="test('<?php echo $a ?>', '<?php echo $c ?>');">LINK</a>
</body>
</html>
谢谢你们!!!