簡體   English   中英

如何在php文件中使用javascript更改父html文件中的圖片src?

[英]How can I use javascript inside of a php file to change an image src in the parent html file?

我有一個php文件中的“與我聯系”框。 php在contact.html文件中的內部運行。 我的目標是,當有人將鼠標懸停在“提交”按鈕上時,contact.html文件中的圖像將發生變化。 提交按鈕在php文件中。 如何制作一個鼠標懸停事件來更改contact.html文件中圖像的src?

在contact.html文件中,我有:

<img id = "me" src="Pictures/Image1.jpg" name="mypic">
<iframe src="contactform/contactform.php" frameborder='0' width='100%' height='600' ></iframe>

我一直在使用的javascript是:

var image1=new Image()
image1.src="Pictures/Image1.jpg"
var image2=new Image()
image2.src="Pictures/Image2.jpg"
var image3=new Image()
image3.src="Pictures/Image4.jpg"

changepic1 = function() {
    document.images.me.src=image1.src
        }

changepic2 = function() {
    document.images.me.src=image2.src
        }

changepic3 = function() {
    document.images.me.src=image3.src
        }

在php文件中,我有:

<div class='container'>
    <input type='submit' name='Submit' value='Submit' />
</div>

使用jQuery處理mouseover事件。 像這樣在您的html中包含jQuery:

 <head> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  
 </head>

在您的iframe中添加一個ID:

 <img id = "me" src="Pictures/Image1.jpg" name="mypic">
 <iframe id = "myframe" src="contactform/contactform.php" frameborder='0' width='100%' height='600' ></iframe>

這是供鼠標懸停切換圖像的:

<script type="text/javascript">
window.onload = function(){

    var image1=new Image()
    image1.src="Pictures/Image1.jpg"
    var image2=new Image()
    image2.src="Pictures/Image2.jpg"
    var image3=new Image()
    image3.src="Pictures/Image4.jpg"

    $('#myframe').contents().find('container').mouseover(function() {
        var thisSrc = $('#me').attr('src');
        var newSrc;

        if(thisSrc == image1.src)
           newSrc = image2.src;
        else if(thisSrc == image2.src)
           newSrc = image3.src;
        else
           newSrc = image1.src;

        $('#me').attr("src", newSrc);
    });
};
</script>

您可以簡單地進行onmouseover事件。 單擊此鏈接作為示例。

您也可以使用jquery來做到這一點。 單擊此鏈接作為示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM