Jquery 文本验证失败
我发现这个问题经常发生,而且不一致 此问题导致jquery.text比较无法工作,因为jquery尝试比较实际措辞但失败 例如:“第二个标题”在浏览器中显示为一行文字,但在查看html源代码时,“第二个”和“标题”位于不同的行中 因此,导致Jquery 文本验证失败,jquery,html,validation,whitespace,Jquery,Html,Validation,Whitespace,我发现这个问题经常发生,而且不一致 此问题导致jquery.text比较无法工作,因为jquery尝试比较实际措辞但失败 例如:“第二个标题”在浏览器中显示为一行文字,但在查看html源代码时,“第二个”和“标题”位于不同的行中 因此,导致if($(this).text()=“第二个标题”)不起作用 在这种情况下,有没有消除html源代码中空白的好方法?这样我可以继续使用。文本比较 显示非工作版本的示例 <table border="0" cellpadding="0" c
if($(this).text()=“第二个标题”)
不起作用
在这种情况下,有没有消除html源代码中空白的好方法?这样我可以继续使用。文本比较
显示非工作版本的示例
<table border="0" cellpadding="0" cellspacing="1">
<tr>
<td width="400" colspan="3" align="center"><span>First Title</span></td>
</tr>
<tr align="center">
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td width="400" colspan="3" align="center"><span>Second
Title</span></td>
</tr>
<tr align="center">
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
$(document).ready(function() {
replaceText();
});
function replaceText()
{
var first, second
first= "", second="";
$("*").each(function() {
if($(this).children().length==0)
{
if($(this).text()=="First Title")
{
first+= jQuery.trim($(this).closest('tr').next('tr').find("td").eq(0).text());
}
if($(this).text()=="Second Title")
{
second+= jQuery.trim($(this).closest('tr').next('tr').find("td").eq(0).text());
}
}
});
alert(first);
alert(second); //fail
}
第一个标题
1.
2.
3.
第二
标题
4.
5.
6.
$(文档).ready(函数(){
replaceText();
});
函数replaceText()
{
第一,第二
第一个=”,第二个=”;
$(“*”)。每个(函数(){
if($(this.children().length==0)
{
if($(this).text()=“第一个标题”)
{
first+=jQuery.trim($(this.closest('tr')。next('tr')。find('td')。eq(0.text());
}
if($(this).text()=“第二个标题”)
{
second+=jQuery.trim($(this).closest('tr').next('tr').find('td').eq(0.text());
}
}
});
警报(第一);
警报(秒);//失败
}
如果不依赖重复的空格(例如,3个空格),可以用一个空格替换所有相邻的空格
function shrinkWhitespace(t) {
return t.replace(/\s+/g, ' ');
}
范例
>> shrinkWhitespace('Second \n\tTitle') == 'Second Title'
true