简体   繁体   English

如何在按钮上单击插入图像

[英]how to insert image on button click

I have create a form, and I want to insert image when I click on the button "tester". 我已经创建了一个表单,并且我想在单击“测试器”按钮时插入图像。 This is my code : 这是我的代码:

<table id="table">
   <tr>
       <td id="col1">
           <form>

               <p><label for="text"> your text</label>:<input type="text" id="text"></input></p>

               <input type="submit" value="tester" onclick="addImage()" />
           </form>
       </td>
       <td id = "img"></td>
   </tr>
</table>




<script>
function addImage(){
    document.getElementById("img").innerHTML = "<img src='newWatermark.png'/>"
}
</script>


<style>
#table{width:100%}
#col1{width:50%}

</style>

It's work but just half a time ... And just during a few second ... I don't understand why 它在工作,但是只有一半的时间...只是在几秒钟内...我不明白为什么

Moreover I think that when I click on the button I refresh the page. 此外,我认为当我单击按钮时,我会刷新页面。 In the URL I have : /watermark.html at the beginning and /watermark.html?your+text= when I click on the button. 在URL我: /watermark.html在开始和/watermark.html?your+text=当我点击的按钮。 But I just want to add an image on the same page. 但是我只想在同一页面上添加图像。 How can I do this? 我怎样才能做到这一点?

You submitting Your form. 您提交表格。

Change type of input from submit to button . 将输入类型从“ submit更改为“ button

To avoid page refresh use input type button not submit 为了避免页面刷新,请使用输入类型buttonsubmit

 function addImage(){ document.getElementById("img").innerHTML = "<img src='newWatermark.png'/>"; //This will overwrite previous image } function appendImage(){ document.getElementById("img").innerHTML += "<img src='newWatermark.png'/>"; //This will append image } 
 #table{width:100%} #col1{width:50%} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table"> <tr> <td id="col1"> <form> <p><label for="text"> your text</label>:<input type="text" id="text"></input></p> <input type="button" value="tester" onclick="addImage()" /> <input type="button" value="Append" onclick="appendImage()" /> </form> </td> <td id = "img"></td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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