for(var index = 0; index < words.length; index++)
{
var currentWord = words[index];
var currentLength = tenLengthString.length;
if(((currentLength + currentWord.length + ((currentLength > 0) ? 1: 0))) > 30)
{
html+= $('.RLTLeftDiv').append('<div style=width:100%; clear:both; line-height:200%;><div style=width:60%; float:left; ><b>'+ hours + ":" + minutes + ":" + seconds+'</b></div><div style=width:40%; float:left; text-align:right; ><b>'+ count +' </b></div></div>')+$('.RLTRightDiv').append(tenLengthString);
console.log(tenLengthString);
count++;
tenLengthString = currentWord;
} else {
if(currentLength > 0)
tenLengthString += " ";
tenLengthString += currentWord;
}
if(index == words.length - 1){
console.log(tenLengthString);
html+='<div>'+tenLengthString+'</div>';
}
}
$("#test").html(html)
答案 0 :(得分:1)
以下部分说明了这一点。这是一个jQuery对象,当你可以使用cat到字符串时,会调用对象的toString()
方法[object Object]
。
html+= $('.RLTLeftDiv').append('<div style=width:100%; clear:both; line-height:200%;><div style=width:60%; float:left; ><b>'+ hours + ":" + minutes + ":" + seconds+'</b></div><div style=width:40%; float:left; text-align:right; ><b>'+ count +' </b></div></div>')+$('.RLTRightDiv').append(tenLengthString);
答案 1 :(得分:0)
问题是你在字符串连接中使用jQuery对象,结果为[object Object]
var $cp = $('<div><div style="width:22%; float: left; font-size:18px; line-height:200%;" class="RLTLeftDiv"><div style="width:100%; clear:both; line-height:200%;"><div style="width:60%; float:left;"><b class="time">hours :minutes:seconds</b></div><div style="width:40%; float:left; text-align:right;"><b><span class="count"></span> </b></div></div></div><div style="width:78%; float:left; font-size:18px; line-height:200%;" class="RLTRightDiv"></div></div>')
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
var str = "Ben Nadel reviews the various approaches to Sachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title among his fans Sachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title among his fansSachin Ramesh Tendulkar is a former Indian cricketer widely acknowledged as the greatest batsman of the modern generation, popularly holds the title among his fans substring";
var words = str.split(" ");
var tenLengthString = "";
var html = '';
var count = 1;
for (var index = 0; index < words.length; index++) {
var currentWord = words[index];
var currentLength = tenLengthString.length;
if (((currentLength + currentWord.length + ((currentLength > 0) ? 1 : 0))) > 30) {
var $clone = $cp.clone();
$clone.find('.time').html(hours + ":" + minutes + ":" + seconds);
$clone.find('.count').html(count);
$clone.find('.RLTRightDiv').html(tenLengthString);
$("#test").append($clone);
console.log(tenLengthString);
count++;
tenLengthString = currentWord;
} else {
if (currentLength > 0) tenLengthString += " ";
tenLengthString += currentWord;
}
if (index == words.length - 1) {
console.log(tenLengthString);
$("#test").append('<div>' + tenLengthString + '</div>');
}
}
演示:Fiddle