我正在尝试添加一些html标签,并将字符串列为javascript中的var。这样我就可以在html中替换以后的代码了。
app.use('/index', function(req, res) {
var query = req.query;
var searchQuery = query.search;
for(i = 0; i < 11; i++){
if (data[i].indexOf(searchQuery) != -1){
console.log(data[i]);
//document.getElementById("".innerHTML = )
newdata.concat("<li>",data[i],"<li><br>");
console.log(newdata);
}
};
console.log(newdata);
var fileContent = fs.readFileSync(path.join(__dirname, '/public/index.html'), "utf8");
res.send(fileContent.replace("{{data}}", newdata));
});
我正在尝试将信息添加到newdata但是concat doens似乎无法正常工作。你能帮帮我,告诉我为什么吗?我在使用concat添加到newdata之前打印数据,但是在additon newdata仍然为空之后。为什么?
以防整个代码在这里
var express = require('express');
var path = require('path');
var app = express();
var port = process.env.PORT || 3000;
var fs = require ('fs')
var data = ['yellow','mellow','hello','yellow jaws','jaws','jaws 2','jaws 3','mellow jaws','angry jaws','angry jews','angry yellow juice'];
var newdata = '';
app.use(express.static(path.join(__dirname, '/public')));
app.use('/index', function(req, res) {
var query = req.query;
var searchQuery = query.search;
for(i = 0; i < 11; i++){
if (data[i].indexOf(searchQuery) != -1){
console.log("VATO");
console.log(data[i]);
//document.getElementById("".innerHTML = )
newdata.concat("<li>",data[i],"<li><br>");
console.log(newdata);
}
};
console.log(newdata);
var fileContent = fs.readFileSync(path.join(__dirname, '/public/index.html'), "utf8");
res.send(fileContent.replace("{{data}}", newdata));
});
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, '/public/index.html'));
});
app.listen(port, function() {
console.log('App is listening on port ' + port);
});
答案 0 :(得分:1)
Concat不会修改原始字符串。它返回修改。改变这个:
newdata.concat("<li>",data[i],"<li><br>");
到
newdata = newdata.concat("<li>",data[i],"</li><br>");
(我也关闭了li标签)