Home > Article > Web Front-end > How to compare strings for equality in jquery
There are many ways to compare whether strings are equal in jQuery. The following are some of the commonly used methods:
var str1 = "hello"; var str2 = "world"; if(str1 === str2){ console.log("字符串相等"); }else{ console.log("字符串不相等"); }
var str1 = " hello "; var str2 = "world"; if($.trim(str1) === str2){ console.log("字符串相等"); }else{ console.log("字符串不相等"); }
var str1 = $("div#div1").text(); var str2 = "world"; if(str1 === str2){ console.log("字符串相等"); }else{ console.log("字符串不相等"); }
var str1 = $("input#input1").val(); var str2 = "world"; if(str1 === str2){ console.log("字符串相等"); }else{ console.log("字符串不相等"); }
In short, there are many ways to compare whether strings are equal in jQuery. You need to choose the appropriate method according to the usage scenario and needs.
The above is the detailed content of How to compare strings for equality in jquery. For more information, please follow other related articles on the PHP Chinese website!