两个图像 - 如果单击图像,则显示其他图像?
我正在尝试找到一种在单击时切换图像的方法。
我有两个图像。
我有一页,在此页面上显示了其中一张图像。
如果我单击图像,我希望将其他图像加载到其位置。
我对html和css了解一点,对php了解一点。我假设正确的方法是使用 javascript。不幸的是,我对此一无所知。
到目前为止,这是我的代码,我想做的是,单击此图像时加载 .../page-2.jpg 。
$pageContent = '
<h2>Promotions</h2>
<div class="promo-img">
<img src="'.$docPath.'/page-1.jpg" alt="Page 1" name="Page 1" width="100%" height="150%" id="page1" style="background: #FFF; display:block;" />
</div>
';
任何意见都将不胜感激,谢谢!
@Dennis
我哪里出了问题?
$pageContent = '
<h2>Promotions</h2>
<div class="promo-img"><img id="promoImage" src="'.$docPath.'/page-1.jpg" alt="Page 1" width="100%" height="150%" style="background: #FFF; display:block;"
onClick="document.getElementById(\'promoImage\').onclick = function() {
if(this.src == \''.$docPath.'/page-1.jpg\'){
this.src = \''.$docPath.'/page-2.jpg\';
} else {
this.src = \''.$docPath.'/page-1.jpg\';
}
};" /></div>
';
好吧,这个好像没有onclick效果吧?
$pageContent = '
<h2>Promotions</h2>
<div class="promo-img"><img id="promoImage" src="'.$docPath.'/page-1.jpg" alt="Page 1" width="100%" height="150%" style="background: #FFF;" /></div>
<script type="text/javascript">
document.getElementById(\'promoImage\').onclick = function() {
if(this.src == \''.$docPath.'/page-1.jpg\'){
this.src = \''.$docPath.'/page-2.jpg\';
} else {
this.src = \''.$docPath.'/page-1.jpg\';
}
};
</script>
';
I am trying to find a way to switch an image on click.
I have two images.
I have one page, on this page I have one of the images displayed.
If I click on the image, I would like the other image to be loaded in its place.
I know a bit about html and css, and a bit less about php. I am assuming the correct way to go about this would be the use of javascript. Unfortunately, I know nothing about this.
Here is my code so far, what I want to do, is have .../page-2.jpg loaded when this image is clicked.
$pageContent = '
<h2>Promotions</h2>
<div class="promo-img">
<img src="'.$docPath.'/page-1.jpg" alt="Page 1" name="Page 1" width="100%" height="150%" id="page1" style="background: #FFF; display:block;" />
</div>
';
Any input here would be greatly appreciated, Thanks!
@Dennis
Where am I going wrong with this?
$pageContent = '
<h2>Promotions</h2>
<div class="promo-img"><img id="promoImage" src="'.$docPath.'/page-1.jpg" alt="Page 1" width="100%" height="150%" style="background: #FFF; display:block;"
onClick="document.getElementById(\'promoImage\').onclick = function() {
if(this.src == \''.$docPath.'/page-1.jpg\'){
this.src = \''.$docPath.'/page-2.jpg\';
} else {
this.src = \''.$docPath.'/page-1.jpg\';
}
};" /></div>
';
Okay, This does not seem to have an onclick effect?
$pageContent = '
<h2>Promotions</h2>
<div class="promo-img"><img id="promoImage" src="'.$docPath.'/page-1.jpg" alt="Page 1" width="100%" height="150%" style="background: #FFF;" /></div>
<script type="text/javascript">
document.getElementById(\'promoImage\').onclick = function() {
if(this.src == \''.$docPath.'/page-1.jpg\'){
this.src = \''.$docPath.'/page-2.jpg\';
} else {
this.src = \''.$docPath.'/page-1.jpg\';
}
};
</script>
';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTML:
Javascript: (点击切换)
Javascript: (悬停切换)
这些是最简单的技术。有一些“更好”的解决方案使用 jQuery 或 CSS 将代码与 HTML 分开。
HTML:
Javascript: (click to switch)
Javascript: (hover to switch)
These are the simplest techniques. There are "better" solutions using jQuery or CSS that separate code from the HTML.