处理xhtml的jQuery和javascript

jQuery and javascript dealing with xhtml

本文关键字:javascript jQuery xhtml 处理      更新时间:2023-09-26

我试着运行这段代码:

$(document).ready(function(){
$("table").append("<tr>");
for(var i=1, i<7, i++){
$("table").append("<td>"+i+"</td>");
}
$("table").append("</tr>");
})

这意味着我想添加一行和7表数据到我的行和关闭行,为此我使用"for",因为它应该与javascript一起使用。

这是一个外部。js文件,给我一个错误;未捕获的SyntaxError:意外的标记<,这是在第三行,比较操作符"<"。但是,如果我不能使用比较运算符!!!!,我怎么使用,对于++,它也会抛出错误。我能做什么?

在for循环中使用;代替,

需要在tr后面添加td

Append将添加整个元素(它的工作方式与字符串连接不同)。所以你不需要分别添加<tr></tr>

$("table").append("<tr></tr>");
for (var i = 1; i < 7; i++) {
    $("table tr").append("<td>" + i + "</td>");
}

小提琴

您的脚本中有语法错误,需要使用;而不是,来定义for(var i=1; i<7; i++)的3个部分

$(document).ready(function(){
    //generate a string 
    var str = "<tr>");
    //Here you need to use ; instead of ,
    for(var i=1; i<7; i++){
        str += "<td>"+i+"</td>";
    }
    str += "</tr>";
    //Append
    $("table").append(str);
})

应该是;,而不是, .使用for(var i=1; i<7; i++)