Joh*_*nyQ 1 javascript php dom
我想比较2个字符串,但没有按照我的意愿进行比较.我从div获取一个值并将其转换为字符串,当我发现它的类型告诉我是一个字符串但仍然通过if语句.
当我写.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>
Run Code Online (Sandbox Code Playgroud)
从您的HTML看起来,您在<?php ...>
指令后有一个尾随空格.无论如何,它都不会完全相同"Ireland"
.尝试使用trim
从字符串末尾删除任何空格.
var newc = String(count).trim();
Run Code Online (Sandbox Code Playgroud)