Javascript 如何在单击链接时显示div
这是我的代码问题是: 当我点击细节链接时,div会快速打开,在隐藏之后,我想不显示 被隐藏。 一切都是好的,问题只在于这句话:Javascript 如何在单击链接时显示div,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,这是我的代码问题是: 当我点击细节链接时,div会快速打开,在隐藏之后,我想不显示 被隐藏。 一切都是好的,问题只在于这句话: echo "<td>"." <a href='computer.php?id=$record[id]' onClick='opendiv();' >Detail</a> "."; 这是css代码: #box{ display:none; width:200px; height:200px; position:absolute;
echo "<td>"." <a href='computer.php?id=$record[id]' onClick='opendiv();' >Detail</a> ".";
这是css代码:
#box{
display:none;
width:200px;
height:200px;
position:absolute;
top:40px;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<script src="script.js"></script>
<?php
//---------------connect to db ----------
include 'connect.php';
$sql="SELECT `id`, `name`, `student`, `teacher`, `year`, `level`, `abstract` FROM `computer` ";
$mydata=mysql_query($sql);
while($record=mysql_fetch_array($mydata)){
include'generalvalue.php';
echo "<td>"." <a href='computer.php?id=$record[id]' onClick='opendiv();' >Detail</a> "."</td>";
}//end while
?>
<div id="box">
<a href="#" onClick="closediv()" >Close</a><br><br>
<?php
if(isset($_GET['id'])){
$id=$_GET['id'];
$sql="SELECT * FROM `computer` where id='$id'";
$mydata=mysql_query($sql);
$row=mysql_fetch_array($mydata);
echo $row[6];
}//end if
?>
</div>
</body>
</html>
当您单击“详细信息”时使用标记。若未使用jquery/java脚本阻止默认事件,则浏览器将根据默认标记行为重定向到浏览器中的computer.php。
您可以使用以下命令:
echo "<td><a class='detail' href='#' data-id='" . $record['id'] . "'> Detail </a></td>";
脚本:
在computer.php文件中,可以使用以下代码来处理ajax请求
if( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest' ) {
if(isset($_POST['id']) && intval($_POST['id']) > 0 ){
$id = $_POST['id'];
$sql = "SELECT * FROM `computer` where id='$id'";
$mydata = mysql_query($sql);
$row = mysql_fetch_array($mydata);
echo json_encode(array("status" => "success", "html" => "<h3>".$row[6]."</h3>"));
die();
} else {
echo json_encode(array("status" => "error", "message" => "Please provide valid computer id to get detail."));
die();
}
}
这个问题与phpy有关吗?你可以在你的函数中传递你的id作为:-我需要这个语句中的id。你可以使用ajax调用使用你的代码从计算机表中获取数据。谢谢兄弟,但我不太明白,你能为我写adjax代码吗,因为我不擅长javascript。
echo "<td><a class='detail' href='#' data-id='" . $record['id'] . "'> Detail </a></td>";
<script>
$(function(){
$("a.detail").click(function(e){
e.preventDefault();
var id = $(this).attr('data-id');
// make ajax call to "computer.php"
$.ajax({
"url": "computer.php",
"data": {"id": id},
"type": "POST",
"dataType": "json",
"success": function(result) {
if(result.status == "success"){
$("#yourDivID").html('').html(result.html);
} else {
$("#yourDivID").html('').html(result.message);
}
//Show your div
$("#yourDivID").show();
},
"error": function(e) {}
});
});
});
</script>
if( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest' ) {
if(isset($_POST['id']) && intval($_POST['id']) > 0 ){
$id = $_POST['id'];
$sql = "SELECT * FROM `computer` where id='$id'";
$mydata = mysql_query($sql);
$row = mysql_fetch_array($mydata);
echo json_encode(array("status" => "success", "html" => "<h3>".$row[6]."</h3>"));
die();
} else {
echo json_encode(array("status" => "error", "message" => "Please provide valid computer id to get detail."));
die();
}
}