如何使用它的ID设置按钮的颜色? [英] How to set color of a button using it's ID?

查看:143
本文介绍了如何使用它的ID设置按钮的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只知道按钮的ID。让我们btnID,现在我需要使用此ID更改此按钮的背景颜色。有可能吗?如果可能请帮助。

解决方案

首先,您需要知道如何使用 ClientID 与propery ClientIDMode 属性,用于获取转换为HTML的值 id 以您希望的方式属性。如果您不清楚这个主题,请阅读:

http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx [<一个href =http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx\"target =_ blanktitle =New Window> ^ ],

http: //msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx [ ^ ]。



其余是纯HTML和JavaScript。您需要使用HTML id 属性的值来查找相关元素。



第一步,您可以创建一个可以动态添加或删除的可选CSS样式。为了便于说明,我们定义两种颜色,普通颜色和高亮颜色,例如:

 normal  { background-color    silver; } 
突出显示 { background-color 黄色; }





在JavaScript中,您可以找到按ID按钮,通过添加,删除或更改样式类来响应某些事件来更改颜色。我可以建议最好的方法是使用jQuery。再说一次,仅举一些工作示例,让我们考虑一下当鼠标开启和退出按钮时我们改变颜色的情况:

 


document )。ready( function (){
myButton =


#someId); // #...是ID描述符
myButton.hover(
function (){ // 鼠标进入:

All I know just ID of a Button. Let's It "btnID", Now I need to change the background color of this button using this ID. Is is possible? If possible Please help.

解决方案

First of all, you need to know how to use ClientID with propery ClientIDMode attributes to get the values translated to HTML id attributes the way you want. If you are not clear about this topic, please read:
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx[^].

The rest is on pure HTML and JavaScript. You need to use the value of the HTML id attribute, to find the element in question.

For the first step, you can create an optional CSS style which you can dynamically add or remove. For purpose of the illustration, let's define two colors, normal and highlight, for example:

.normal { background-color: silver; }
.highlight { background-color: yellow; }



In JavaScript, you can find the button by ID and change the color by adding, removing or changing the style class in response to some event. The best way to do it I could advise would be using jQuery. Again, just for some working example, let's consider the case when we change the colors when mouse goes on and out of the button:


(document).ready(function() { myButton =


("#someId"); // "#..." is the ID descriptor myButton.hover( function() { // mouse goes in:


这篇关于如何使用它的ID设置按钮的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆