将if / else转换为JS / jQuery

时间:2018-03-25 11:44:45

标签: javascript php

我需要先将PHP数组更改为JS数组:

$url = "web.com";

<script type="text/javascript">
  var url = <?php echo json_encode($url); ?>;
</script>

现在使用Javascript / JQuery,我需要执行此操作:

if(strpos($url, "com") !== false){
  echo "<p><b style="background-color:blue; color:white;">TEST</b></p>";
}
else{
  echo "<p><b style="background-color:yellow; color:white;">TEST</b></p>";
}

我该怎么做?

1 个答案:

答案 0 :(得分:-1)

var url = location.href;

var child = document.createElement('div');
if(url.indexOf("com") != -1) {  
  str = '<p><b style="background-color:blue; color:white;">TEST</b></p>'; 
} else {
  str = '<p><b style="background-color:yellow; color:white;">TEST</b></p>';
}

child.innerHTML = str;
child = child.firstChild;
document.getElementById('test').appendChild(child);
<div id="test"></div>