简体   繁体   English

如何在javascript中一起创建带有图片和标题的按钮

[英]how to create button with Image & caption together in javascript

Hi i want to create a button which has caption (ex Category) and the Image of it in the Button too. 嗨,我想创建一个具有标题(例如类别)的按钮,以及它在按钮中的图像。

so we can say that button has both Caption + Image 所以我们可以说该按钮同时具有Caption + Image

I hav to use javascript only 我只能使用javascript

This creates button with javascript and put lable "Category" to it. 这将创建带有javascript的按钮,并在其中添加标签“ Category”。

var btn = document.CreateElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "Category");

var frm = document.forms[0];
frm.appendChild(btn);

You can't have Text + Image to button as far as i know, either you can have text or image. 据我所知,您不能将“ 文本+图像”按钮,或者可以具有文本或图像。 You can specify an image for it like this though: 您可以像这样指定图像:

var btn = document.CreateElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "Category");
btn.style.backgroundImage = "url(path)"; // for image

var frm = document.forms[0];
frm.appendChild(btn);

.... ....

Other Possibility: 其他可能性:

If you want to apply Text + Image, then you should create an image containing both of these and specify that as background for the button. 如果要应用文本+图像,则应创建一个包含这两个图像的图像,并将其指定为按钮的背景。

Update: 更新:

If you set padding on the button to account for the width of the background image and set the background image to no-repeat you can have both. 如果您在按钮上设置填充以说明背景图片的宽度,并将背景图片设置为不重复,则可以同时使用两者。

Suggested By Andrew in the comments below, thanks 由安德鲁在下面的评论中建议,谢谢

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

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