Home >Web Front-end >JS Tutorial >Implementation code of automatic test paper layout system based on jQuery_jquery
Implementation code of automatic test paper layout system based on jQuery_jquery
WBOYOriginal
2016-05-16 18:12:361240browse
Generate a multi-page test paper based on the provided test paper questions (which is a clean HTML webpage with only "data"). The user can turn pages and has a time limit for answering questions. The questions in the test paper displayed to the user need to occupy as little space as possible. (For example, in multiple-choice questions, two shorter options are merged from two lines to one). The same question should not be displayed across pages to facilitate the answerers. The administrator can change the style of the test paper (font, color, line spacing, page margins, Like word processing software...), some explanatory text can be inserted between the questions (such as informing the respondent of the instructions for answering, etc.). The question title, multiple-choice question options, and explanatory text can contain multimedia information (text, pictures, lists, tables, videos, etc...). There is no limit to the number of multiple-choice options and no limit to single-choice or multiple-choice questions. Page turning should have customizable animation effects
The test paper sample provided is similar to the following (Input):
5. What kind of teacher do you think is a good teacher?
ol>
Thinking What should we do in the face of this demand? Using JavaScript too, it seems. Later, I decided to use jQuery and Aptana as IDE (although the jQuery support library cannot be installed on Windows, it will be fine if I change the OS, which is strange), and I will use CSS for formatting.
Specific steps:
Import the test paper question HTML Format all multiple-choice questions, divide a row into four positions, and try to fit the options into one position, two positions, or four Position (that is, the effect of four items in a row, two items in a row, or one item in a row) Paging all questions The idea is still clear, but due to the large number of browsers, it is still quite troublesome, and I am a novice. Before I came into contact with jQuery...
Implement page file (different from the example, but the same format)
/** * * @param {String} PaperOlId the id value of the ol tags indicating pages. * @param {String} ProblemClass the css class name for problem area. * @param {String} DescClass the css class name for description area. * @param {String} ChoicesClass the css class name for choices area. * @param {String} LeftPageId the id of the left page. * @param {String} RightPageId the id of the right page. * @author ExSystem */ function TTestPaperProcessor(PaperOlId, ProblemClass, DescClass, ChoicesClass, LeftPageId, RightPageId) { this.FPaperOlId = PaperOlId; this.FProblemClass = ProblemClass; this.FDescClass = DescClass; this.FChoicesClass = ChoicesClass; this.FLeftPageId = LeftPageId; this.FRightPageId =RightPageId; $('#' + this.FLeftPageId).html(''); $('#' + this.FRightPageId).html(''); this._FormatProblemOptions(); this._DivideIntoPages(); this.setCurrPage(1); }
TTestPaperProcessor.prototype = { FPaperOlId: '', //the id property of the ol tag contains the whole test paper. FProblemClass: '', //the css class name for problem area. FDescClass: '', //the css class name for description area. FChoicesClass: '', //the css class name for choices area. FLeftPageId: '', //the left page. FRightPageId: '', //the right page. CPageClass: 'Page', FIsDisplayTableSupported: null, //whether the browser is the EVIL M$IE6,7 that does not support display: table(-cell). FCurrPage: 0, //start from 1, 0 for no page has been displayed yet. FPageCount: 0, //page count. // /** // * Get external css stylesheet info. // * @param {String} Selector The selector in the css style sheet. // * @param {String} Property The property name. // * @return {String} The value of the property, or null for undefined property. // */ // _GetCssInfo: function(Selector, Property) { // var mCss = document.styleSheets[0].cssRules || document.styleSheets[0].rules; // for (var mIndex = 0; mIndex < mCss.length; ++mIndex) { // if (mCss[mIndex].selectorText.toLowerCase() == Selector) { // return mCss[mIndex].style[Property]; // } // } // return null; // },
/** * Formats radios and checkboxes for the Choices quiz. */ _FormatProblemOptions: function() { var mThis = this; var mSelector = '.' + this.FProblemClass + ' .' + this.FChoicesClass; $(mSelector).each(function() { //Rearrange the options for each problem ordered by offsetWidth of the label tag. var mLabels = new Array(); mLabels = jQuery.makeArray($('label', this)); mLabels.sort(function(First, Second) { return $(Second).outerWidth(true) > $(First).outerWidth(true); }); $(mLabels).appendTo(this);
//Layout the options into the appropreate form. var mSlots = -1; //Force to create a new row, inside the while() loop. var mSlotWidth = $(mSelector).width() / 4.0; var mCellSize = 0; if (mThis._IsDisplayTableSupported()) { while (mLabels.length > 0) { //alert($(mLabels[0]).outerWidth(true) + '::' + $(mLabels[0]).outerHeight(true) + '::' + $(mLabels[0]).html()); if (mSlots <= 0) { //If no empty slot, create a new row. mCurrRow = $(''); mCurrRow.appendTo(this); mSlots = 4; mCellSize = 0;
var mRealCellWidth = $(mLabels[0]).outerWidth(true); if (mRealCellWidth < mSlotWidth) { mCellSize = 1; } if (mRealCellWidth >= mSlotWidth && mRealCellWidth < mSlotWidth * 2) { mCellSize = 2; } if (mRealCellWidth >= mSlotWidth * 2) { mCellSize = 4; } } mSlots -= mCellSize; if (mSlots >= 0) { //If empty slots exists, put the cell into the row. mLabel = mLabels.shift(); $(mLabel).addClass('___cell'); $(mLabel).css('display', 'table-cell'); $(mLabel).appendTo(mCurrRow); } } $('.___table').each(function() { //Align all the tables and cells. $(this).css('width', '100%'); var mCellWidth = 100 / $('.___cell', this).length; $('.___cell', this).css('width', mCellWidth + '%'); }); } else { // for the evil M$IE6, use table, tr, td tags. while (mLabels.length > 0) { if (mSlots <= 0) { //If no empty slot, create a new row. mCurrRow = $('
var mRealCellWidth = $(mLabels[0]).attr('offsetWidth'); //The EVIL IE only: //be sure to use this css reset: table { border-collapse: collapse; border-spacing: 0; } //otherwise, 2 lines will be occupied by some long problem options instead of 1. //or use this code instead: var mRealCellWidth = $(mLabels[0]).attr('offsetWidth') * 1.3; if (mRealCellWidth <= mSlotWidth) { mCellSize = 1; } if (mRealCellWidth > mSlotWidth && mRealCellWidth <= mSlotWidth * 2) { mCellSize = 2; } if (mRealCellWidth > mSlotWidth * 2) { mCellSize = 4; } } mSlots -= mCellSize; if (mSlots >= 0) { //If empty slots exists, put the cell into the row. mLabel = mLabels.shift(); mCell = $('
'); $(mLabel).appendTo(mCell); mCell.appendTo($('tr', mCurrRow)[0]); } } $('.___table').each(function() { //Align all the tables and cells. $(this).css('width', '100%'); var mCellWidth = 100 / $('tbody tr .___cell', this).length; $('tbody tr .___cell', this).css('width', mCellWidth + '%'); }); } }); },
/** * Create a new page, and add it to the paper. * @return {jQuery} the new page. */ _CreateNewPage: function() { ++this.FPageCount;
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn