简体   繁体   English

使用javascript用嵌入式PHP编辑html

[英]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)) ),并使用具有idgetElementById来操作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.

相关问题 使用chrome开发人员工具在html页面中编辑嵌入式Javascript - Editing Embedded Javascript in html page using chrome developer tools 使用 Javascript 编辑 HTML DOM - Editing HTML DOM using Javascript 如何在使用php,javascript,html的嵌入式产品中使用推技术? - How to use push technology with embedded product using php, javascript, html? PhP使用嵌入式JavaScript从HTML中提取条纹 - PhP extracting striing from HTML with embedded JavaScript 具有嵌入式javascript的php在HTML标签上输出换行符? - php with embedded javascript outputs newlines on HTML tags? 将 JavaScript 变量传递给嵌入在 php 中的 html 时遇到问题 - Trouble passing JavaScript variables to html that is embedded in php PHP中嵌入的HTML中嵌入的Javascript函数无法正常工作? - Javascript function embedded in HTML that is embedded in PHP not working properly? 使用Javascript函数在html中编辑表格的单元格 - Editing cells of a table in html using Javascript function 使用调用php函数(从sql数据库获取信息)的javascript编辑html。 - editing html using javascript that calls php function that gets information from sql database.. complicated… 使用PHP和Javascript编辑上传的PDF文件 - Editing uploaded PDF file using PHP and Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM