[英]editing html with embedded PHP using javascript
I hope someone has an answer for me, 我希望有人对我有答案,
I am currently trying to create a PHP product page for my shop website, I have an sql table that stores the name of an image prefix eg if the image file is 'test_1.png' then the table stores 'test'. 我目前正在尝试为我的商店网站创建一个PHP产品页面,我有一个sql表,用于存储图像前缀的名称,例如,如果图像文件为“ test_1.png”,则该表存储“ test”。 using embedded php
使用嵌入式PHP
src="images/shop/<?php echo $row['item_img'], '_1.png';?>"></img>
what I would like to do is using js, dynamically update the src on a mouse click. 我想做的是使用js,只需单击鼠标即可动态更新src。
something like eg. 像。
var imgSwitch = function(i){
Document.getElementById('js-img').src = "images/shop/
<?php echo $row['item_img'], '_';?>i<?php echo '.png';?>";
}
Even to me this seems wrong which is why I've turned to the GURU's here 即使在我看来,这似乎是错误的,这就是为什么我转向这里的GURU的原因
Is there anyway this would be possible? 反正有可能吗? If not, any suggestions would be GREATLY appreciated
如果没有,任何建议将不胜感激
I am trying to figure out what you are asking, and I think this is your way to go: 我正在尝试弄清楚您的要求,我认为这是您的选择:
var imgSwitch = function(i){
document.getElementById('js-img').src = "images/shop/<?php echo $row['item_img'], '_';?>" + i + ".png";
}
The change is in the i, you have to cut the string and add it as a variable. 更改在i中,您必须剪切字符串并将其添加为变量。
But remember that the PHP code is executed at the server, and will not change once the page is sent to the client. 但是请记住,PHP代码在服务器上执行,并且一旦页面发送到客户端就不会更改。 When you execute that function, $row['item_img'] will always be the same.
当您执行该函数时,$ row ['item_img']将始终相同。
A simple example which you can adapt. 一个可以适应的简单示例。 What I do in the code below is give the element an
id
and attach an onclick
to it. 我在下面的代码中所做的是为该元素提供一个
id
并为其附加一个onclick
。
In the function we pass the id
as a parameter ( onclick(changeSrc(this.id))
) and we manipulate the src using the getElementById
as we have the id
. 在函数中,我们将
id
作为参数传递( onclick(changeSrc(this.id))
),并使用具有id
的getElementById
来操作src。
<img src="http://ladiesloot.com/wp-content/uploads/2015/05/smiley-face-1-4-15.png" id="test" onclick="changeSrc(this.id);" height="400" width="400" />
<script>
function changeSrc(id) {
document.getElementById(id).src = "http://i0.wp.com/www.compusurf.es/wordpress/wp-content/uploads/2014/04/smiley.jpeg?fit=1200%2C1200";
}
</script>
http://jsfiddle.net/tq1Lq5at/ http://jsfiddle.net/tq1Lq5at/
Edit 1 编辑1
You're using Document
when it should be document
, notice the lowercase d . 您使用的
Document
时,它应该是document
,注意小写字母d。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.