的index.html
<html>
<head>
<script type="text/javascript" src = "jquery-1.10.1.js"></script>
<script type="text/javascript" language = "javascript">
function swapContent(cv)
{
$_("#myDiv").html("Put animated .gif here").show();
var url= "myphpscript.php";
$_post(url,{contentVar:cv},function(data){
$_("#myDiv").html(data).show();
});
}
</script>
</head>
<body>
<a href = "#" onClick = "return false" onmousedown = "javascript.swapContent('Con1')"> Content1 </a> •
<a href = "#" onClick = "return false" onmousedown = "javascript.swapContent('Con2')"> Content2 </a> •
<a href = "#" onClick = "return false" onmousedown = "javascript.swapContent('Con3')"> Content3 </a> •
<div id = "myDiv"> My Default Content 1</div>
</body>
</html>
myphpscript.php
<html>
<body>
<?php
$_contentVar = $_POST['contentVar'];
if($_contentVar == 'Con1')
{
echo ' My Defaut Content';
}
else if($contentVar == 'Con2')
{
echo ' My Defaut Content 2';
}
else if($contentVar == 'Con3')
{
echo ' My Defaut Content 3';
}
?>
</body>
</html>
我在onmousedown
事件完成时尝试显示一些动态内容。我还没有完成动画,但只是我希望在选择不同的链接时更改所需的divs
,但是它似乎不起作用。 jQuery 文件已正确加载。
答案 0 :(得分:0)
COndition错误
$_contentVar = $_POST['contentVar'];
if( $_contentVar == 'Con1') //here you are using $contentVa not $_contentVar
答案 1 :(得分:0)
我不确定你为什么在$_("#myDiv")..
中使用过_我认为你的javascript代码应该是
function swapContent(cv)
{
$("#myDiv").html("Put animated .gif here").show();
var url= "myphpscript.php";
$.post(url,{contentVar:cv},function(data){
$("#myDiv").html(data).show();
});
}
我看到的另一个问题是如何调用该函数应该是javascript:swapContent(&#39; Con1&#39;)而不是javascript.swapContent(&#39; Con1&#39;)。你放了&#39;。&#39;而不是&#39;:#39;
所以链接应该是
<a href = "#" onClick = "return false" onmousedown = "javascript:swapContent('Con1')"> Content1 </a> •
<a href = "#" onClick = "return false" onmousedown = "javascript:swapContent('Con2')"> Content2 </a> •
<a href = "#" onClick = "return false" onmousedown = "javascript:swapContent('Con3')"> Content3 </a> •
您还应该在PHP脚本中更改变量名称,我希望您已经知道