【问题标题】:How do I use an image for a button instead of standard input type button?如何将图像用作按钮而不是标准输入类型按钮?
【发布时间】:2013-11-18 11:45:41
【问题描述】:

我有这个代码:

<input type="button" onclick="alert('A message here');" value="Button"/>

如何使用我自己的图像按钮而不是标准按钮?

【问题讨论】:

    标签: html image button


    【解决方案1】:
    <img src="{{your image url here}}" onclick="alert('A message here');" />
    

    <div onclick="alert('A message here');" style="background: url({{your image url here}});background-repeat:no-repeat; width:150px; height:100px;"></div>
    

    【讨论】:

      【解决方案2】:

      如果你使用&lt;button&gt;标签而不是&lt;input type="button"&gt;,你可以用HTML内容填充它:

      <button onclick="alert('A message here');" type="button">
        <img src="myurl">
      </button>
      

      文档:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

      示例:http://jsbin.com/ezeFaYU/1/edit

      【讨论】:

        【解决方案3】:

        本身没有图像按钮,但由于您使用的是 javascript,因此您可以将点击事件添加到图像中。请参阅下面的小提琴。

        <img class="button" src="http://goo.gl/UGQl0i" width="100" height="100" onclick="alert('A message here');" >
        

        http://jsfiddle.net/CN8Js/

        【讨论】:

          【解决方案4】:

          这应该有效。只需将“img.png”替换为您的图片即可。

          <img src = "img.png" onclick="alert('A message here');"/>
          

          【讨论】:

            猜你喜欢
            • 2017-11-23
            • 2011-02-24
            • 1970-01-01
            • 2013-07-19
            • 2012-09-25
            • 2014-10-30
            • 1970-01-01
            • 2018-01-24
            • 1970-01-01
            相关资源
            最近更新 更多