PHP JavaScript回声不起作用

时间:2016-11-01 20:32:13

标签: javascript php html

这是我的PHP / JS代码:

echo "<script>
function transformCard() {
document.getElementById('cardImage').src = " . $row['transformedimageurl'] . "
document.getElementById('transformbtn').onClick = deformCard();
}
function deformCard() {
document.getElementById('cardImage').src = " . $row['imageurl'] . "
document.getElementById('transformbtn').onClick = transformCard()
}
</script>";

我的HTML:

<button id="transformbtn" onClick="transformCard()"></button>
<img id='cardImage' src="image.jpg" />

按钮点击不会更改图像的src

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

src放在引号中:

echo "<script>
    function transformCard() {
    document.getElementById('cardImage').src = '" . $row['transformedimageurl'] . "';
    document.getElementById('transformbtn').onClick = deformCard();
}
function deformCard() {
    document.getElementById('cardImage').src = '" . $row['imageurl'] . "';
    document.getElementById('transformbtn').onClick = transformCard()
}
</script>";

您可能还想删除()处理程序上的onClick

document.getElementById('transformbtn').onClick = deformCard;
document.getElementById('transformbtn').onClick = transformCard;