所以我不得不问一个问题,因为它还没有让我发表评论。 我在这里使用RC提供的代码作为答案 Change image source with javascript
但是因为我的文件不是.html,它的.php和我的html代码都是echo,源代码最终看起来像这样:
这里是.php文件中包含的js
<script>
function backColor(a) {
document.getElementById("back_image").src=a;
}
</script>
(谢谢RC!)
这里是.php
<?php
$cush_patterns = array();
$cush_patterns[101] = array(
"pattern" => "Ocean Blue",
"img" => "img/cush/blue.gif",
"color" => "blue",
"price" => 22.22
);
$cush_patterns[102] = array(
"pattern" => "Yellow sun",
"img" => "img/cush/yellow.gif",
"color" => "yellow",
"price" => 28.99
);
echo '<img id="back_image" src="img/cush/blue.gif">';
echo '<ul>';
echo'<li>';
echo '<img class="cush_showcase" src="' . $cush_pattern["img"] . '" onclick="backColor("' . $cush_pattern[img] . '");">';
echo '</li>';
echo '</ul>';
?>
这是我运行它时得到的源代码
<ul class="cush_show">
<li>
<img class="cush_showcase" src="img/cush/blue.gif" onclick="backColor("img/cush/blue.gif");">
<h6 id="patt_name">Ocean Blue</h6>
<h6 id="prices">$22.22</h6>
</li>
</ul>
<ul class="cush_show"><li>
<img class="cush_showcase" src="img/cush/yellow.gif" onclick="backColor("img/cush/yellow.gif");">
<h6 id="patt_name">Yellow sun</h6>
<h6 id="prices">$28.99</h6>
</li>
</ul>
这可能是显而易见的事情,但是我不能把手指放在它上面而且它让我疯了。
提前谢谢!
答案 0 :(得分:0)
所以这是我在互联网上搜索后的解决方法。感谢@Tamil Selvan,@查尔斯约翰汤普森三世的评论,他们也帮助了!
<?php
$cush_patterns = array();
$cush_patterns[101] = array(
"pattern" => "Deep ocean blue",
"img" => "img/cush/blue.gif",
"color" => "blue",
"price" => 29.99
);
$cush_patterns[102] = array(
"pattern" => "Spring yellow",
"img" => "img/cush/yellow.gif",
"color" => "yellow",
"price" => 28.99
);
echo '<img id="back_image" src=\'' . $cush_patterns["img"] . '\'>';
echo '<ul class="cush_show">';
echo '<li>';
echo '<img class="cush_showcase" alt="Your patterns" src=\'' . $cush_pattern["img"] . '\' onclick="backColor(\'' . $cush_pattern["img"] . '\')">';
echo '</li>';
echo '</ul>';
?>