基于php变量更改图像的javascript

时间:2013-12-13 22:10:35

标签: javascript php jquery html

<div style="margin-left:5%;width:5%;float:left;margin-right:10%;">
  <img src="./1.png" id="imgstatus"/>
</div>
<div style="width:80%;float:left;">
  <h3>Your transaction was <?php echo $message; ?> ! Your transaction reference number for any furthur communication is <?php echo $cust_ref_no; ?> .</h3>
</div>
</div>

我有这个HTML。我想要的只是javascript,它会在<img src="./1.png" />时将img src更改为if $message='succes' <img src="./2.png"/>$message='failed'

2 个答案:

答案 0 :(得分:2)

您可以使用三元运算符来最小化代码长度。

<div style="margin-left:5%;width:5%;float:left;margin-right:10%;">
  <img src="<?php echo ($message == 'success' ? 'success.jpg' : 'failed.jpg'); ?>" id="imgstatus"/>
</div>
<div style="width:80%;float:left;">
  <h3>Your transaction was <?php echo $message; ?> ! Your transaction reference number for any furthur communication is <?php echo $cust_ref_no; ?> .</h3>
</div>
</div>

答案 1 :(得分:0)

<div style="margin-left:5%;width:5%;float:left;margin-right:10%;">
  <img src="<?php

  if($message == 'success')
  {
      echo 'success.jpg';
  }
  else
  {
      echo 'failed.jpg';
  }

  ?>" id="imgstatus"/>
</div>
<div style="width:80%;float:left;">
  <h3>Your transaction was <?php echo $message; ?> ! Your transaction reference number for any furthur communication is <?php echo $cust_ref_no; ?> .</h3>
</div>
</div>