简体   繁体   中英

equal expression doesn't work as expected in JS

My first expression

 alert($('.dateTimeHolder').html()); //returned Date - Time *

My second

 alert($('.dateTimeHolder').html()  == 'Date - Time *'); return false

Why the second expression is false?

一旦前导和尾随空格都是可能的,请尝试使用trim()删除它们

alert($('.dateTimeHolder').html().trim() == 'Date - Time *');

You have to trim the html you are comparing to a static string, there could be some leading/trailing spaces/newline chars.

alert($.trim($('.dateTimeHolder').html())  == 'Date - Time *'); // return false

 alert($.trim($('.datetime').html()) === "date - time*"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='datetime'> date - time* </div> 

使用这个trim()

 alert($('.dateTimeHolder').html().trim()  == 'Date - Time *'); return false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM