Javascript cakephp更改图像onclick,放置id和函数调用
我试图在cakephp中更改一个图像onclick,但是如果我的语法在放置id和添加onclick函数时是正确的,我就不知道了。以下是我的代码,如有任何帮助,将不胜感激Javascript cakephp更改图像onclick,放置id和函数调用,javascript,cakephp,onclick,Javascript,Cakephp,Onclick,我试图在cakephp中更改一个图像onclick,但是如果我的语法在放置id和添加onclick函数时是正确的,我就不知道了。以下是我的代码,如有任何帮助,将不胜感激 <?php echo $this -> Html -> link($this -> Html -> image('button_on', array('height' => '40','width' => '70')).''.('ready'), array('id' =>
<?php
echo $this -> Html -> link($this -> Html -> image('button_on', array('height' => '40','width' => '70')).''.('ready'), array('id' => 'changeimg', 'onclick' => 'changeImage'), array('escape' => false)); ?>
<script language = "javascript">
function changeImage() {
if (document.getElementById("changeimg").src === "button_on") {
document.getElementById("changeimg").src === "button_off";
} else {
document.getElementById("changeimg").src === "button_on";
}
}
</script>
函数changeImage(){
if(document.getElementById(“changeimg”).src==“按钮打开”){
document.getElementById(“changeimg”).src==“按钮关闭”;
}否则{
document.getElementById(“changeimg”).src==“按钮打开”;
}
}
检查HTML输出,确保Cake正确生成链接和图像。我不确定ready
部件的用途,但可以根据需要插入。您应该有这样的代码:
echo $this->Html->link(
'#', // specify a target
$this->Html->image(
'button_on',
array(
// height and width attributes are deprecated
// use CSS styles in style attribute instead
'style' => 'height: 40px; width: 70px;',
'id' => 'changeimg',
'onclick' => 'changeImage()'
)
),
array('escape' => false) // this goes in the link part, not the image
);
<script>
function changeImage() {
if (document.getElementById("changeimg").src === "button_on") {
document.getElementById("changeimg").src = "button_off";
} else {
document.getElementById("changeimg").src = "button_on";
}
return false; // prevent the link's default behaviour
}
</script>
那么您的Javascript应该如下所示:
echo $this->Html->link(
'#', // specify a target
$this->Html->image(
'button_on',
array(
// height and width attributes are deprecated
// use CSS styles in style attribute instead
'style' => 'height: 40px; width: 70px;',
'id' => 'changeimg',
'onclick' => 'changeImage()'
)
),
array('escape' => false) // this goes in the link part, not the image
);
<script>
function changeImage() {
if (document.getElementById("changeimg").src === "button_on") {
document.getElementById("changeimg").src = "button_off";
} else {
document.getElementById("changeimg").src = "button_on";
}
return false; // prevent the link's default behaviour
}
</script>
函数changeImage(){
if(document.getElementById(“changeimg”).src==“按钮打开”){
document.getElementById(“changeimg”).src=“button\u off”;
}否则{
document.getElementById(“changeimg”).src=“按钮打开”;
}
return false;//防止链接的默认行为
}
注意:==
是一个严格的比较运算符,因此当您将src
属性指定给其他对象时,可以使用赋值运算符=