我正在尝试更改标签文字。但作为id,我无法理解我错过了什么,因为它没有将文本从英语更改为西班牙语。任何人都可以帮忙解决这个问题吗?
<td width="125">
<span class='Normal'>
<span class="FormText">
<label for='txt4506'>First Name:</label>
</span>
<span class='required'>*</span>
</span>
</td>
我想改变这种方式。
$(document).ready(function() {
var tds = document.querySelectorAll('td');
[].forEach.call(tds, function(td) {
if (td.textContent.trim() == 'First Name:')
{
td.textContent = 'Nome:';
}
});
});
答案 0 :(得分:1)
调试101:查看您要匹配的值
console.log(td.textContent.trim());
给出:
First Name:
*
trim()
仅删除尾随和前导空格。
改为标记标签,并使用正则表达式匹配您关注的位:
$(document).ready(function() {
var tds = document.querySelectorAll('label');
[].forEach.call(tds, function(td) {
td.textContent = td.textContent.replace(/First Name/, "Nome");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td width="125">
<span class='Normal'>
<span class="FormText">
<label for='txt4506'>First Name:</label>
</span>
<span class='required'>*</span>
</span>
</td>
</tr>
</table>
答案 1 :(得分:-2)
//A much understandable answer!!
<td width="125">
<span class='Normal'>
<span class="FormText">
<label for='txt4506'>First Name:</label>
</span>
<span class='required'>*</span>
</span>
</td>
<script>
$(document).ready(function() {
var tds=$('span>label');
for(var i=0; i<tds.length; i++){
if($(tds[i]).text().match(/\bFirst Name\b/)){
console.log($(tds[i]).text()+' is matched!');
//now do what u want...
$(tds[i]).text('New text')
}
}
});
</script>
答案 2 :(得分:-3)
我认为你在if语句中缺少冒号。尝试:
if (td.textContent.trim() == 'First Name:')
答案 3 :(得分:-3)
你必须选择唯一的。这是每个人在选择DOM元素时所做的事情(不是这样吗?;))。所以这样做:
var elem = document.querySelector("label[for='txt4506']");
elem.innerHTML = "some text";