【问题标题】:trying to compare 2 string if statement [closed]试图比较 2 个字符串 if 语句 [关闭]
【发布时间】:2014-03-03 19:58:54
【问题描述】:

我正在尝试比较 2 个字符串,但没有我想要的比较。我从 div 获取一个值并将其转换为字符串,当我发现它告诉我的 typeof 是一个字符串但它仍然通过 if 语句传递。

当我 write.document 时,它们看起来都一样。 谢谢

<div id="countries"><?php $geoplugin->locate(); echo"{$geoplugin->countryName}"?> </div> 

<script>
                 var country = document.getElementById("countries");
                 var count = country.innerHTML;
                 var newc = String(count);


                 if ( newc === "Ireland" ){
                    document.write("this is ireland");
                    alert("You are from Ireland");

                 }else{
                    document.write("You Live in " + newc);
                                    //alert(typeof newc);                  
                    //alert(newc);

                 }

                 </script>

【问题讨论】:

  • &lt;?php $geoplugin-&gt;locate(); echo"{$geoplugin-&gt;countryName}"?&gt; 应该是 &lt;?php $geoplugin-&gt;locate(); echo $geoplugin-&gt;countryName; ?&gt;,缺少 ; 并且缺少空格
  • 题外话:你应该把""+count改为increase the speed of execution,而不是String(count)

标签: javascript php dom


【解决方案1】:

在您的 HTML 中,您似乎在 &lt;?php ...&gt; 指令之后有一个尾随空格。所以无论那里有什么,它都不会完全等于"Ireland"。尝试使用trim 删除字符串末尾的所有空格。

var newc = String(count).trim();

【讨论】:

  • 谢谢 p.s.w.g 那是某个地方的空白。
猜你喜欢
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多