php - 设计投票系统

标签 php javascript jquery html css

我在这里制作了一个示例投票系统:Sample Voting System

然而,在反复尝试之后,我无法按照我想要的方式设置样式,因为开发人员在这里使用了太多的 div。要么字体太大,要么与数字重叠,要么不成比例,等等。

我希望它看起来像这样:

Styling the Voting system

这是我的代码:

    <!DOCTYPE html>
<head>
<title>Sample Polling System</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".vote").click(function() 
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if(name=='up')
{
$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
type: "POST",
url: "up_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}  
}); 
}
else
{
$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
type: "POST",
url: "down_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
return false;
});
});
</script>

<style type="text/css">
body
{
font-family:'Georgia', Times New Roman, Times, serif;
}
#main
{
height:100px; 
width:800px;
}
a
{
color:#DF3D82;
text-decoration:none;

}
a:hover
{
color:#DF3D82;
text-decoration:underline;

}
.up
{
height:40px; font-size:24px; text-align:center; background-color:gray; margin-bottom:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
.up a
{
color:#FFFFFF;
text-decoration:none;

}
.up a:hover
{
color:#FFFFFF;
text-decoration:none;

}
.down
{
height:40px; font-size:24px; text-align:center; background-color:gray; margin-top:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}

.down a
{
color:#FFFFFF;
text-decoration:none;

}
.down a:hover
{
color:#FFFFFF;
text-decoration:none;

}
.box1
{
float:left; height:80px; width:50px;
}
.box2
{
float:left; width:440px; text-align:left;
margin-left:10px;height:60px;margin-top:10px;
font-size:18px;
}
img
{
border:none;
padding-top:7px;
}
</style>
</head>
<body>
<div align="center">
<h3>Sample Polling System</h3><hr>
<?php
include('config.php');
$sql=mysql_query("SELECT * FROM Messages  LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg=$row['msg'];
$mes_id=$row['mes_id'];
$up=$row['up'];
$down=$row['down'];
?>
<div id="main">
<div class="box1">
<div class='up'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="up"><?php echo $up; ?></a><!--Tried placing here, didn't work--></div>
<div class='down'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="down"><?php echo $down; ?></a></div>
</div><!-- tried to put this box below box2 but still didn't work -->
<div class='box2' ><?php echo $msg; ?></div>
</div><!-- tried atleast 10 permutations and combinations, couldn't get it work-->
<hr>
<?php
}
?>
</div>
</body>
</html>

请帮我得到我想要达到的效果。

谢谢

最佳答案

我修改了你的代码:

    <!DOCTYPE html>
<head>
<title>Sample Polling System</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$(".vote").click(function() 
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if(name=='up')
{
$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
type: "POST",
url: "up_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}  
}); 
}
else
{
$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
type: "POST",
url: "down_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
return false;
});
});
</script>

<style type="text/css">
body
{
font-family:'Georgia', Times New Roman, Times, serif;
}
#main
{
width:800px;
overflow:hidden;
}
a
{
color:#DF3D82;
text-decoration:none;

}
a:hover
{
color:#DF3D82;
text-decoration:underline;

}
.up
{
height:40px; font-size:24px; text-align:center; background-color:gray; margin-bottom:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px; float:left; width:40px; 
}
.up a
{
color:#FFFFFF;
text-decoration:none;

}
.up a:hover
{
color:#FFFFFF;
text-decoration:none;

}
.down
{
height:40px; font-size:24px; text-align:center; background-color:gray; margin-top:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px; float:left; margin-left:40px; width:40px; 
}

.down a
{
color:#FFFFFF;
text-decoration:none;

}
.down a:hover
{
color:#FFFFFF;
text-decoration:none;

}
.box1
{
float:left; clear:both;
}
.box2
{
float:left; width:440px;  text-align:left;
font-size:18px; 
}
img
{
border:none;
padding-top:7px;
}
</style>
</head>
<body>
<div align="center">
<h3>Sample Polling System</h3><hr>
<?php
include('config.php');
$sql=mysql_query("SELECT * FROM Messages  LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg=$row['msg'];
$mes_id=$row['mes_id'];
$up=$row['up'];
$down=$row['down'];
?>
<div id="main">
<div class='box2' ><?php echo $msg; ?></div>
<div class="box1">
<div class='up'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="up"><?php echo $up; ?></a></div><span style="margin-top:10px; float:left; margin-left:10px;">People Agree to This</span>
<div class='down'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="down"><?php echo $down; ?></a></div><span style=" margin-top:10px; float:left; margin-left:10px;">People Disagree to This</span>
</div><!-- tried to put this box below box2 but still didn't work -->

</div><!-- tried atleast 10 permutations and combinations, couldn't get it work-->
<hr>
<?php
}
?>
</div>
</body>
</html>

检查一下 http://jsfiddle.net/qCwZd/

#box2 是标题放在#box1上面,我给了它一个float:left;#box1 得到一个 float:left; 和一个 clear:both; 所以它可以显示在一个新行上。

我在每个灰色框旁边添加了一个span,第一个是People agree to this,其他People disagree to this,他们是给定一个 float:left; 放在灰色框旁边,还有一些 margin-left。我删除了一些 height 属性。

关于php - 设计投票系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14535168/

相关文章:

jQuery UI 可排序滚动辅助元素偏移 Firefox 问题

php - 使用 MySQL 和 PHP 检查线程中有多少帖子的最快方法

PHP:如何将 0777(八进制)转换为字符串?

javascript - 如何知道当前鼠标位置有哪些元素?

javascript - HTML中 "template"标签、 "content"标签和自定义元素标签的区别

javascript - jquery中如何给元素添加id

javascript - 将 Chart.js 比例限制为整数

php - 如何让用户为不同的事件连续创建相同的 HTML 表单?

php - Sentry v2.10 与 Laravel 6.20.27 (PHP v7.4.19) 的 SSL 错误

javascript - Date.parse 无法正常工作