简体   繁体   中英

Change Element inside element with Onclick or Hover property

<html>
  <head>
    <style>
      #container {position:relative;left:15%;}
      #myImage {width:65%;height:280px;}
      #text {padding-top: 50px;}
      #textbox {width:65%;height:280px;position:absolute;top:0;left:0;}
    </style>
  </head>

  <body>
    <div id="container" onclick="changeImage()" >
      <img id="myImage" src="C:\Documents and Settings\svarghe1\My Documents\Downloads\Jaguar_Xj_Saloon_4.jpgj_.jpg" alt="">
      <div id="textbox">
        <p style="color:white;text-align:center;margin-top:50px;"class="text">Jaguar_Xj_Saloon</p>

        <script>
          function changeImage() {
            if (document.getElelmentById("container").innerHTML="myImage") {
              container.appendChild(myImage)
            } else {
              container.appendChild(textbox)
            }
          }
        </script>
      </div>
    </div>
  </body>
</html>

Here, I am trying to create a script for Sharepoint site page to change an element in div from Image to textbox with Onclick or hover property. There might be a lot of mistakes as this is my first attempt on JS . I have also tried

<script> 
function changeImage() {
    var image= document.getElementById("myImage");
    if (image=true) {
       var element = document.getElementById("container");
       var UImage = document.createElementById("myImage");
       element.appendChild(UImage)
    } else {
       var element = document.getElementById("container");
       var Utextbox = document.createElementById("textbox");
       element.appendChild(UImage)
       element.appendChild(Utextbox);
    }
}
</script>


#container:hover #myImage{ display:none; } 

I have tried the code above in CSS , instead of script. It didn't work. At the same time the code,

a:hover #box{ text-decoration: none;color:green;background-color: Turquoise;cursor:pointer }

Works really fine. Why is that? I have given class instead of id . It also didn't work. It works in ordinary HTML file. But can't get to work in Sharepoint site. So, can you help?

Instead of appending Image and text box from the javascript, you can already write the html codes for both and do the hide/show according to your need. I think it will make things lot easier and neat.

As for the Hover property, you can attach event of hover with jquery. You can check the following link: http://api.jquery.com/hover/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM