【问题标题】:Styling a voting system设计投票系统
【发布时间】:2021-06-20 02:18:59
【问题描述】:

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

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

我希望它看起来像这样:

这是我的代码:

    <!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>

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

谢谢

【问题讨论】:

  • 有什么问题?我看到的唯一区别是您的选项是并排的,在页面上,它们是垂直的。这是唯一的问题吗?
  • 首先:您在该页面中有多个ID。不酷。如果你能做点什么(或让开发者做点什么),那就去做吧。

标签: php javascript jquery html css


【解决方案1】:

我修复了你的代码:

    <!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,第一个表示人们同意这一点,其他人们不同意这一点,他们得到了一个float:left;放在灰色盒子旁边,还有一些margin-left。我删除了一些 height 属性。

【讨论】:

  • 非常感谢您的代码。但是非常感谢下面的解释。我弄乱了你的解释并成功地得到了我想要的:)
【解决方案2】:

首先,您应该也更改您的标记。您正在使用多个具有相同名称('main')的 elemtn ID,这是错误的。在一个 HTML 页面中,相同 ID 的元素宽度不应超过 1 个。

我建议您执行以下操作,而不是您的标记:

<div align="center">
<h3>Sample Polling System</h3><hr>
<div id="main">

 <div class="row">
  <div class="wrapper">
   <div class="item">
    <div class="box1">
     <div class='up'><a href="" class="vote" id="13" name="up">2</a></div>
     <div class='down'><a href="" class="vote" id="13" name="down">0</a></div>
    </div><!--end box1-->
    <div class='box2' >Sachin Tendulkar is GOD</div>
   </div><!--end item-->

  <div class="item">
    <div class="box1">
     <div class='up'><a href="" class="vote" id="13" name="up">2</a></div>
     <div class='down'><a href="" class="vote" id="13" name="down">0</a></div>
    </div><!--end box1-->
    <div class='box2' >Sachin Tendulkar is GOD</div>
   </div><!--end item-->
 </div><!--end wrapper-->
</div><!--end row-->

</div><!--end main-->

然后你的 css 可以简单地是:

#main{width:100%;}
.row{width:100%; border-bottom:1px solid black;}
.wrapper{width:800px; margin:0 auto;}
.item{width:400px; float:left;}

您可以在此处查看一个工作示例:http://jsfiddle.net/gHmDv/

【讨论】:

    【解决方案3】:

    我在这里做了一个投票系统。我从 Stackoverflow 的投票中获取了颜色。我用剪辑路径制作了“图标”,并给了它们一个三角形。使用单选按钮,我将标签与名称属性连接起来,如果您现在单击三角形,则单选按钮被激活并且三角形变为橙色。这是代码(你可以省略javascript代码):

    var number = 0;
    var count = document.getElementById("voteCounter");
    
    function plusOne() {
      number = 1;
      count.innerHTML = number;
    }
    
    function minusOne() {
      number = - 1;
      count.innerHTML = number;
    }
    * {
      margin: 0;
      padding: 0;
      font-family: 'Roboto', sans-serif;
    }
    
    #vote {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 50px;
      font-size: 25px;
      color: #6C737B;
    }
    
    #upvote {
      background: #BCC0C3;
      height: 50px;
      width: 40px;
      clip-path: polygon(50% 60%, 0% 100%, 100% 100%);
      margin-bottom: 10px;
      cursor: pointer;
    }
    
    #downvote {
      background: #BCC0C3;
      height: 50px;
      width: 40px;
      clip-path: polygon(50% 40%, 100% 0%, 0% 0%);
      margin-top: 10px;
      cursor: pointer;
    }
    
    input[type="radio"] {
      display: none;
    }
    
    input[id="upvoteRadio"]:checked ~ #upvote {
      background: #E5863D;
    }
    
    input[id="downvoteRadio"]:checked ~ #downvote {
      background: #E5863D;
    }
    <div id="vote">
      <input type="radio" id="upvoteRadio" name="vote" value="upvote">
      <label for="upvoteRadio" id="upvote" onclick="plusOne()"></label>
      <p id="voteCounter">0</p>
      <input type="radio" id="downvoteRadio" name="vote">
      <label for="downvoteRadio" id="downvote" onclick="minusOne()"></label>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多