Javascript 如何比较jQuery中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8346948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare strings in jQuery
提问by Jeroen Bal
In this code I am trying to compare two strings with jQuery. The current content of a div and the new content, loaded from a (currently) static page.
在这段代码中,我试图用 jQuery 比较两个字符串。div 的当前内容和从(当前)静态页面加载的新内容。
But it doesn't work, when the loaded content is the same as the current content, it still loads the new content...
但它不起作用,当加载的内容与当前内容相同时,它仍然加载新内容......
When I view the content vars, they really are equal (I used the JS alert).
当我查看内容变量时,它们确实是相等的(我使用了 JS 警报)。
$(document).ready(function () {
var refreshId = setInterval(function () {
$.get("ajaxGetEvents.aspx", function (data) {
if ($('#events').html() != data) {
$('#events').fadeOut('slow', function () {
$(this).load('ajaxGetEvents.aspx').fadeIn('slow');
});
} else {
alert("item is equal");
}
});
}, 5000);
});
采纳答案by Abe Miessler
I suspect you are not getting the values you expect for $('#events').html()
and data
.
我怀疑你没有得到你期望的值$('#events').html()
和data
。
Can you use firebugor developer toolsto place a breakpoint at your if statement and inspect the values?
您可以使用firebug或开发人员工具在 if 语句中放置断点并检查值吗?
Alternatively you could just throw in a couple of alerts to see what these contain... Example:
或者,您可以只添加几个警报以查看其中包含的内容...示例:
alert($('#events').html());
alert(data);
if ($('#events').html() != data) {
That said, if you are not familiar with using firebug or developer tools, now might be a good time to start using them. They will save you a lot of headaches in the future and they are infinitely better than alerts when it comes to debugging.
也就是说,如果您不熟悉使用 firebug 或开发人员工具,现在可能是开始使用它们的好时机。它们将在未来为您省去很多麻烦,而且在调试方面,它们比警报要好得多。
回答by Manuel van Rijn
maybe it's beter to compare the .text()
instead of the (maybe generated) html
也许最好比较.text()
而不是(可能生成的)html
so you have
所以你有了
$("#events").text() == $(data).text()
回答by Cyclonecode
If your ajax request return a normal text string (not html formated), I think you should use text()
instead of html()
.
如果你的Ajax请求返回一个正常的文本字符串(不是HTML格式化),我认为你应该使用text()
代替html()
。
$(document).ready(function () {
var refreshId = setInterval(function () {
$.get("ajaxGetEvents.aspx", function (data) {
if ($('#events').text() != data) {
$('#events').fadeOut('slow', function () {
$(this).load('ajaxGetEvents.aspx').fadeIn('slow');
});
} else {
alert("item is equal");
}
});
}, 5000);
});
回答by Augustus Kling
Are you sure the whitespaces are identical? Let's suppose data
is <span >test</span>
:
你确定空格是一样的吗?让我们假设data
是<span >test</span>
:
var data = "<span >test</span>";
//?Temporarily place your data in a DOMElement to trigger parsing and re-serialization
alert(data===jQuery('<div>').append(test).html());
As you will see, the code above results in false
. The reason is a space after the word span
. Building a DOM tree removes such whitespaces and calling html
does not have a way to restore them.
正如您将看到的,上面的代码导致false
. 原因是单词后有一个空格span
。构建 DOM 树会删除此类空白,并且调用html
无法恢复它们。
Note that this answer is just a guess. Please post your HTML to get more useful/targeted answers.
请注意,这个答案只是一个猜测。请发布您的 HTML 以获得更多有用/有针对性的答案。