jquery - add image to button in javascript - Stack Overflow

I have created a button in javascript and I would like to use an image for the button instead of text,

I have created a button in javascript and I would like to use an image for the button instead of text, but I have not yet found a way to do this. So far the sources I have referenced are not working for me for some reason. What I have so far is as follows

Javascript

var sb=doc.createElement('input');              
sb.type='button';
//sb.value='Find';
sb.src = "url(/Resources/Icons/appbar.next.rest.png)";
sb.style.height='100%';
//sb.style.width='20%';

where my image is locoated in the directory /Resources/Icons/appbar.next.rest.png in my project. Can someone point me in the right direction as to how to properly use an image for a button? I am new to javascript so any links, code, or directions would be greatly appreciated!

I have created a button in javascript and I would like to use an image for the button instead of text, but I have not yet found a way to do this. So far the sources I have referenced are not working for me for some reason. What I have so far is as follows

Javascript

var sb=doc.createElement('input');              
sb.type='button';
//sb.value='Find';
sb.src = "url(/Resources/Icons/appbar.next.rest.png)";
sb.style.height='100%';
//sb.style.width='20%';

where my image is locoated in the directory /Resources/Icons/appbar.next.rest.png in my project. Can someone point me in the right direction as to how to properly use an image for a button? I am new to javascript so any links, code, or directions would be greatly appreciated!

Share Improve this question edited Apr 13, 2012 at 5:35 sujal 1,0503 gold badges18 silver badges29 bronze badges asked Apr 13, 2012 at 3:54 MatthewMatthew 4,06617 gold badges70 silver badges132 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 1
Use input type="image", or use a button element with an <img> child.

for example:

    <input type="image" src="art/clips/eightbll.gif" 
     name="input_img" width="63" height="64">

or

    <button type="button" id="input_img"> 
    <img src="art/clips/eightbll.gif" 
     alt="Image input control" width="63" height="64">
    </button>

for adding image to button you can change type of input from button to image:

var body = document.getElementsByTagName("body")[0];
var s = document.createElement("input");
s.src = "http://www.gravatar./avatar/a4d1ef03af32c9db6ee014c3eb11bdf6?s=32&d=identicon&r=PG";
s.type = "image";
body.appendChild(s);

you can modify it.

http://jsfiddle/6HdnU/

Based on this page which remends against input type="image" it seems like using a type="image" could be problem if you're planning on doing anything with the button (on the server or in javascript.

Basically, instead of using the pure-html properties of the buttons to show the input, you should use CSS's background-image property.

js matching yours:

var sb=doc.createElement('input');              
sb.type='button';
sb.class='myImageButton';

and with this css:

input.myImageButton {
  border: none;
  cursor: pointer;
  display: inline-block;
  text-indent: -3000px;
  background: url(/Resources/Icons/appbar.next.rest.png) no-repeat 50% 50%;
  /* width: 20%; */
  height: 100%;
}

should work.

It's probably worth pointing out that if you're not doing anything with the value of this button then this answer isn't really worth your time. But you had better be sure.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745116426a4612152.html

相关推荐

  • jquery - add image to button in javascript - Stack Overflow

    I have created a button in javascript and I would like to use an image for the button instead of text,

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信