Javascript 如何使用jQuery在HTML中正确打印变量

Javascript 如何使用jQuery在HTML中正确打印变量,javascript,jquery,html,Javascript,Jquery,Html,我使用以下函数打印javascript变量的内容 function message (){ $("#description").html("Candidate " + (lowest+1) + " was the first to get eliminated ending on " + results[lowest][0]+ "%"); } 这与预期的一样正常工作,但是如果我尝试以下操作: function message (){ $("#des

我使用以下函数打印javascript变量的内容

  function message (){
         $("#description").html("Candidate " + (lowest+1) + " was the first to get eliminated ending on " + results[lowest][0]+ "%");   

  }
这与预期的一样正常工作,但是如果我尝试以下操作:

  function message (){
    $("#description").html("Candidate " + (lowest+1) + " was the first to get eliminated ending on " + results[lowest][0]+ "%");
    $("#description").html("Candidate " + (lowest2+1) + " was the second to get eliminated ending on " + results[lowest2][0]+ "%");

  }
这显然行不通。第二条消息覆盖第一条消息的文本。显示这两条消息的正确方式是什么

function message (){
    var output;
    output = "Candidate " + (lowest+1) + " was the first to get eliminated ending on " + results[lowest][0]+ "%";
    output += "Candidate " + (lowest2+1) + " was the second to get eliminated ending on " + results[lowest2][0]+ "%";
    $("#description").html(output);

  }
尽可能少地进行DOM操作,这样可以避免不必要的页面重新绘制:只需使用一个变量来包含所有字符串,并插入一次,以避免多次调用jQuery函数

尽可能少地进行DOM操作,这样可以避免不必要的页面重新绘制:只需使用一个变量来包含所有字符串,并插入一次,以避免对jQuery函数进行多次昂贵的调用。

使用:

使用:


html方法将替换选择器中的所有内容。。。。我想您需要添加到现有html结尾的append,html方法将替换选择器中的所有内容。。。。我认为您需要append,它将添加到现有html的末尾,而不是替换display元素的html

比如说:

 $("#description").append("Candidate " + (lowest+1) + " was the first to get eliminated ending on " + results[lowest][0]+ "%" + "<br />");

然后,您可以根据需要设置div.result的样式,而不必担心换行等问题。

您需要追加而不是替换display元素的html

比如说:

 $("#description").append("Candidate " + (lowest+1) + " was the first to get eliminated ending on " + results[lowest][0]+ "%" + "<br />");
然后您可以根据需要设置div.result的样式,而不用担心换行等问题。

使用

   $("#description").html("Candidate " + (lowest+1) + " was the first to get eliminated ending on " +    results[lowest][0]+ "%");
  $("#description").append("Candidate " + (lowest2+1) + " was the second to get eliminated ending on " + results[lowest2][0]+ "%");
使用

   $("#description").html("Candidate " + (lowest+1) + " was the first to get eliminated ending on " +    results[lowest][0]+ "%");
  $("#description").append("Candidate " + (lowest2+1) + " was the second to get eliminated ending on " + results[lowest2][0]+ "%");