响应式HTML表单

时间:2016-02-26 06:02:02

标签: javascript html css forms css3

我使用以下css代码:

html {
    background: #2B2B2B url(images/bg.gif) repeat;
}
body {
    max-width: 1000px;
    margin: 0px auto;
    font-family: sans-serif;
    margin: 0 auto;
}
header,
footer,
aside {
    display: block;
}
h1 {
    text-align: center;
    color: rgba(0, 0, 0, 0.6);
    text-shadow: 2px 8px 6px rgba(0, 0, 0, 0.2), 0px -5px 35px rgba(255, 255, 255, 0.3);
    text-decoration: underline;
}
label {
    display: block;
}
fieldset {
    border: 0px dotted red;
    width: 400px;
    margin: 0 auto;
}
input,
select {
    width: 400px;
    height: 30px;
    border-radius: 5px;
    padding-left: 10px;
    font-size: 14px;
}
select {
    line-height: 30px;
    background: #f4f4f4;
}
button {
    font-size: 14px;
    padding: 5px;
    background: #333333;
    color: #FFFCEC;
    float: right;
    width: 100px;
}
button:hover {
    font-size: 16px;
}
#edit {
    background: #DC5B21;
}
#delete {} #course,
#name,
#profesor,
#subject {
    background: #ABDCD6;
}
label {
    font-size: 15px;
    font-weight: bold;
    color: #282827;
}
table {
    border-spacing: 0.5rem;
    border-collapse: collapse;
    margin: 0 auto;
    background: #ABDCD6;
}
th {
    background: #E9633B;
}
th,
td {
    border: 2px solid black;
    padding: 10px;
}
td {
    font-weight: bold;
    font-style: oblique;
}
tr:nth-child(even) {
    background: #ABDCD6
}
tr:nth-child(odd) {
    background: #DCD8CF
}
.container {
    width: 1000px;
    margin: 0 auto;
}
.headerbar {
    width: 988px;
    float: left;
}
.headerbar.top {
    background: linear-gradient(45deg, rgba(255, 102, 13, 1) 3%, rgba(255, 109, 22, 1) 32%, rgba(255, 121, 38, 1) 77%, rgba(255, 121, 38, 1) 100%);
    min-height: 100px;
    border-radius: 19px 30px 0px 0px;
    box-shadow: #938D94 7px 7px 5px;
}
.headerbar.bottom {
    background: linear-gradient(45deg, rgba(255, 102, 13, 1) 3%, rgba(255, 109, 22, 1) 32%, rgba(255, 121, 38, 1) 77%, rgba(255, 121, 38, 1) 100%);
    min-height: 60px;
    border-radius: 25px;
    border-radius: 0px 0px 37px 34px;
    box-shadow: #938D94 7px 1px 5px;
}
.leftbar {
    width: 50%;
    background: #EB593C;
    min-height: 605px;
    float: left;
    border-radius: 4px;
    border: 3px dashed #282827;
}
.rightbar {
    width: 47%;
    background: #221E1D;
    min-height: 595px;
    float: left;
    padding: 5px;
    border: 2px solid #EB593C;
    box-shadow: #938D94 5px 5px 5px;
}
#submit,
#clear {
    border-radius: 25px;
}
input:focus {
    border: 1px solid #FF9933;
}
@media screen and (max-width: 700px) {
    .leftbar,
    .rightbar {
        float: none;
    }
    .headerbar.top h1 {
        margin-left: 50px;
        text-align: center;
        float: left;
    }

这是我的HTML页面非常简单

<!DOCTYPE html>
<html>
    <head>
        <title>My web app</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="mystyle2.css" rel="stylesheet"/>
        <script>
            var studentsArray = [];
            var selectedIndex = -1;
            function init() {
                document.getElementById("tablerows").innerHTML = "";
                if (localStorage.studentsRecord) {
                    studentsArray = JSON.parse(localStorage.studentsRecord);
                    for (var i = 0; i < studentsArray.length; i++) {
                        prepareTableCell(i, studentsArray[i].course, studentsArray[i].name, studentsArray[i].profesor, studentsArray[i].subject);
                    }
                }
            }
            function onRegisterPressed() {
                if(validate()){
                var course = document.getElementById("course").value;
                var name = document.getElementById("name").value;
                var profesor = document.getElementById("profesor").value;
                var subject = document.getElementById("subject").value;
                var stuObj = {course: course, name: name, profesor: profesor, subject: subject};
                if (selectedIndex === -1) {
                    studentsArray.push(stuObj);
                } else {
                    studentsArray.splice(selectedIndex, 1, stuObj);
                }
                localStorage.studentsRecord = JSON.stringify(studentsArray);
                init();
                onClarPressed();
            }else{

            }
            }
            function prepareTableCell(index, course, name, profesor, subject) {
                var table = document.getElementById("tablerows");
                var row = table.insertRow();
                var courseCell = row.insertCell(0);
                var nameCell = row.insertCell(1);
                var profesorCell = row.insertCell(2);
                var subjectCell = row.insertCell(3);
                var actionCell = row.insertCell(4);
                courseCell.innerHTML = course;
                nameCell.innerHTML = name;
                profesorCell.innerHTML = profesor;
                subjectCell.innerHTML = subject;
               actionCell.innerHTML = '<button id="edit" onclick="onEditPressed(' + index + ')">Edit</button><br/><button id="delete" onclick="deleteTableRow(' + index + ')">Delete</button>';
            }
            function deleteTableRow(index) {

                studentsArray.splice(index, 1);
                localStorage.studentsRecord = JSON.stringify(studentsArray);
                init();
            }
            function onClarPressed() {
                selectedIndex = -1;
                document.getElementById("course").value = "";
                document.getElementById("name").value = "";
                document.getElementById("profesor").value = "";
                document.getElementById("subject").value = "Math";
                document.getElementById("submit").innerHTML = "Register";
            }

            function onEditPressed(index) {
                selectedIndex = index;
                var stuObj = studentsArray[index];
                document.getElementById("course").value = stuObj.course;
                document.getElementById("name").value = stuObj.name;
                document.getElementById("profesor").value = stuObj.profesor;
                document.getElementById("subject").value = stuObj.subject;
                document.getElementById("submit").innerHTML = "Update";

            }


             function validate(){

                var errors = [];


                var re = /^[\w]+$/;
                var id = document.getElementById("course");
                if(id.value==="" ){
                    errors.push("Course name is empty");
                }else if(id.value.length<3){
                    errors.push("Course name is to shoort");
                }else if(!re.test(id.value)){
                    errors.push("Input contains invalid characters");
                }

                var name = document.getElementById("name");
                var regEx = /^[a-zA-Z ]+$/; 
                if(name.value===""){
                    errors.push("Name cannot be empty");
                }else if(!regEx.test(name.value)){
                   errors.push("Name contains invalid characters");
                }

                var profesor = document.getElementById("profesor");
                if(profesor.value===""){
                    errors.push("Professor field cannot be empty");
                }else if(!regEx.test(profesor.value)){
                    errors.push("Professor field contains invalid characters");
                }

                if(errors.length>0){
                    var message = "ERRORS:\n\n";
                    for(var i = 0;i<errors.length;i++){
                        message+=errors[i]+"\n";
                    }
                    alert(message);
                    return false;
                }

                return true;
            }



        </script>
    </head>
    <body onload="init()">





            <header class="headerbar top"><h1>ITEC3506: Assignment#2</h1></header>



            <aside class="leftbar">


                <div>

                    <fieldset>
                        <label for="course"><span>Course Name</span></label>
                        <input type="text" placeholder="enter name of course" id="course">
                    </fieldset>
                    <fieldset>
                        <label for="name">Your Name</label>
                        <input type="text" placeholder="enter your name" id="name">
                    </fieldset>
                    <fieldset>
                        <label for="profesor">Course Professor</label>
                        <input type="text" placeholder="enter course Professor" id="profesor">
                    </fieldset>
                    <fieldset>
                        <label for="subject">Subject</label>
                        <select id="subject">
                            <option value="Math">Math</option>
                            <option value="Physics">Physics</option>
                            <option value="Chemistry">Chemistry</option>
                            <option value="English">English</option>
                            <option value="CS">CS</option>
                        </select>
                    </fieldset>
                    <fieldset>
                        <label for="submit"> </label>
                        <button id="submit" onclick="onRegisterPressed()">Submit</button>
                        <button id="clear" onclick="onClarPressed()">Clear</button>
                    </fieldset>
                </div>

            </aside>

            <aside class="rightbar">
                <table id="regtable">
                    <thead>
                        <tr>
                            <th>Course</th>
                            <th>Student</th>
                            <th>Professor</th>
                            <th>Subject</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody id="tablerows">
                    </tbody>
                </table>
            </aside>
            <footer class="headerbar bottom"></footer>
        </div>
    </body>
</html>

我的问题是如何将此代码转换为响应式网站。 一切都正常调整,除了我似乎无法调整我的表格和形式。有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

这里发生了一些事情。

首先,您在几个字段上没有设置宽度,因此请更改:

fieldset{
    border: 0px dotted red;
    width: 400px;
    margin: 0 auto;
}

为:

fieldset{
    border: 0px dotted red;
    width: 400px;
    margin: 0 auto;
    max-width: 100%;
}

同时将.headerbarwidth: 988px;更改为width: 100%;

对于响应式框架,您需要确保在没有确保附加width的情况下永远不会设置固定max-width,否则您的内容大小永远不会低于您的大小固定宽度。

其次,我注意到以下内容:

.leftbar{
    width: 50%;
    background: #EB593C;
    min-height: 605px;
    float: left;
    border-radius: 4px;
    border: 3px dashed #282827;
}

你没有特别说出来,但是当我在较小的视图中查看你的代码时,我注意到你的width: 50%;导致背景偏离,这似乎不是你的意图。我建议在.leftbar { width: 100%; }

中添加.rightbar { width: 100%; }@media screen and (max-width:700px){

刚刚离开table。表格不会自动分解,因此在开发响应式网站时通常不是我们想要使用的东西,但当然有时候没有解决这个问题。

有几种方法可以解决table的问题。一种是将table设置为display:block;并在overflow-x: scroll;内部应用@media screen and (max-width:700px){,这样,当用户从较小的屏幕查看时,用户可以向左/向右滚动。另一个是使用可以实现此目的的各种Javascript插件之一。

希望这有助于您走上正轨。祝你好运!

答案 1 :(得分:0)

请勿为这些

设置宽度
input,select{/*width: 400px;*/}
fieldset{/*width: 400px;*/}

如果您明确设置宽度,则无法获得响应式布局