我有一个HTML表和这样的javascript:
var x = document.querySelectorAll("td.status");
if(x[0].textContent = "3")
{
x[0].textContent = "Declined"
}
else if(x[0].textContent = "2")
{
x[0].textContent = "Approved"
}
else
{
x[0].textContent = "Pending"
}
<table>
<tr>
<td>2016-10-11 13:10:26</td>
<td>name</td>
<td>mail@yahoo.com</td>
<td>number</td>
<td class="status" value="3">3</td>
</tr>
<tr>
<td>2016-10-11 13:10:26</td>
<td>name</td>
<td>mail@yahoo.com</td>
<td>number</td>
<td class="status" value="2">2</td>
</tr>
<tr>
<td>2016-10-11 13:10:26</td>
<td>name</td>
<td>mail@yahoo.com</td>
<td>number</td>
<td class="status" value="0">0</td>
</tr>
</table>
当我运行代码时,结果只影响表的第一行。
问题是,当页面加载时,如何使javascript代码用给定的语句替换类status
中的文本:
3 =拒绝了
2 =批准
0 =待定
答案 0 :(得分:1)
if条件不正确,one =用于赋值,==用于比较
因此,在您的情况下,您的if条件必须如下所示
if(x[0].textContent == "3")
答案 1 :(得分:0)
你必须迭代,因为它用jQuery标记,你可以让它为你做任何事情
$('td.status').text(function(_, txt) {
return txt === "3" ? "Declined" :
txt === "2" ? "Approved" : "Pending"
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>2016-10-11 13:10:26</td>
<td>name</td>
<td>mail@yahoo.com</td>
<td>number</td>
<td class="status" value="3">3</td>
</tr>
<tr>
<td>2016-10-11 13:10:26</td>
<td>name</td>
<td>mail@yahoo.com</td>
<td>number</td>
<td class="status" value="2">2</td>
</tr>
<tr>
<td>2016-10-11 13:10:26</td>
<td>name</td>
<td>mail@yahoo.com</td>
<td>number</td>
<td class="status" value="0">0</td>
</tr>
</table>
&#13;
答案 2 :(得分:0)
用于循环
var x = document.querySelectorAll("td.status");
for(var i=0 ; i < x.length; i++ ) {
if(x[i].textContent == "3"){
x[i].textContent = "Declined"
}
else if(x[i].textContent == "2"){
x[i].textContent = "Approved"
}
else {
x[i].textContent = "Pending"
}
}