将单个变量拆分为多个,并在HTML中分别调用它们

时间:2017-04-29 10:12:09

标签: javascript html

我希望根据距离产生估计的步行和驾驶时间。

function handleMeasurements(event) {
var geometry = event.geometry;
var units = event.units;
var order = event.order;
var measure = event.measure;
var measure_corrected = measure / 1.67;
var element = document.getElementById('output');
var out = "";

// Walking time from distance.
var walking_time_mins = (((measure_corrected / 4) * 60).toFixed(2));
var minutes = (walking_time_mins % 60);
var hours = (walking_time_mins - minutes)/60;   


if(order == 1) {
out += "measure: " + measure_corrected.toFixed(3) + " " + units + "Walking time (hh.mm.ss)" + hours + "." +  minutes;
      } else {
out += "measure: " + measure_corrected.toFixed(3) + " " + units + "<sup>2</" + "sup>";
}
element.innerHTML = out;
}

此代码位于.js文件中,而html页面上的“var out;”变量由<div id="output"></div>调用 一切都运转良好,但'变种';结果全都在一条线上。我想将"measure: " + measure_corrected.toFixed(3) + " " + units +""Walking time (hh.mm.ss)" + hours + "." + minutes;"分成表格中的不同单元格。我从互联网上的例子中得到了很多这些代码,我无法弄清楚<div id="output"></div>如何称之为'var out;'。它似乎没有连接任何地方。任何人都可以就如何拆分'var out'提供任何建议进入不同的变量以及如何在html页面中单独调用它们?谢谢。

2 个答案:

答案 0 :(得分:2)

您可以尝试替换下面的

if(order == 1) {
out += "measure: " + measure_corrected.toFixed(3) + " " + units + "Walking time (hh.mm.ss)" + hours + "." +  minutes;
      } else {
out += "measure: " + measure_corrected.toFixed(3) + " " + units + "<sup>2</" + "sup>";
}

out="<table><tr>";
if(order == 1) {
out += "<td>measure: " + measure_corrected.toFixed(3) + " " + units + "</td><td>Walking time (hh.mm.ss)" + hours + "." +  minutes+"</td>";
      } else {
out += "<td colspan=2>measure: " + measure_corrected.toFixed(3) + " " + units + "<sup>2</" + "sup></td>";
}
out+="</tr></table>";

答案 1 :(得分:0)

想出来,关键是getElementID标签。

//ADD LIST ITEM
$("#listForm").submit(function(ev) {
    ev.preventDefault();
    var $listInput = $("#listInput");
    var input = $listInput.val();

    if (input == "") {
        alert("Please enter the item name, then click 'Add'.");
    } else {
        listCount++;

        $("<div />")
            .attr("id", "input" + listCount + "container")
            .attr("class", "inputContainer")
            .appendTo("#checkboxContainer");

        $("<input />")
            .attr("id", "input" + listCount)
            .attr("type", "checkbox")
            .attr("class", "inline checkbox")
            .appendTo("#input" + listCount + "container");

        $("<label />") //create new label
            .attr("id", "label" + listCount) //set ID
            .attr("for", "input" + listCount) //set For
            .attr("class", "inline")
            .attr("data-roll", "none")
            .html(input) //set contents
            .appendTo("#input" + listCount + "container");//add to checkbox container

        $("<img />")
            .attr("id", "closeBtn" + listCount)
            .attr("class", "closeBtn right")
            .attr("src", "assets/android-close.png")
            .appendTo("#input" + listCount + "container");


        //Store the list count
        localStorage.setItem("listCount", listCount);

        //Store the list title
        localStorage.setItem("input" + listCount, input); //"Note " + noteCount + ": " + 

        this.submit();

        $( ":mobile-pagecontainer" ).pagecontainer( "change", "#article3");
    }
});

这是html表代码;

function handleMeasurements(event) {
            var geometry = event.geometry;
            var units = event.units;
            var order = event.order;
            var measure = event.measure;
            var measure_corrected = measure / 1.67;
            var wtime = document.getElementById('walking_time');
            var dtime = document.getElementById('driving_time')
            var element = document.getElementById('output');

            var out = "";
            var out_time = "";
            var out_drive = "";

            // Walking time from distance.
            var walking_time_mins = (((measure_corrected / 4) * 60).toFixed(2));
            var minutes = (walking_time_mins % 60);
            var minutes_corrected = (minutes).toFixed(2);
            var hours = (walking_time_mins - minutes)/60;   

            // Driving time from distance.
            //var driving_time_mins = (((measure_corrected / 30) * 60).toFixed(2));


            if(order == 1) {
                out += measure_corrected.toFixed(3) + " " + units;  
                out_time +=  "(hh.mm.ss)" + hours + "." +  minutes_corrected;
            } else {
            out += "measure: " + measure_corrected.toFixed(3) + " " + units + "<sup>2</" + "sup>";
            }


            element.innerHTML = out;
            wtime.innerHTML = out_time;

}