javascript - 点击按钮,显示css类名

标签 javascript jquery html dom

当我单击按钮时(例如,input type="button"class="test"), 我想显示样式表类名称。

我已经尝试过这样的。

$(this).get(0).style.name

但我只收到未定义的消息。 请告诉我如何获取它。

@@编辑

让我向您展示我的编码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>      
<script type="text/javascript" src="jquery-1.6.4.js"></script>  
<script type="text/javascript" src="jquery-fieldselection.js"></script>

<script>
    $(document).ready(function(){
        var CurrentTextBoxID = "";          

        // toggles the keyboard to show or hide when link is clicked
        $(":text").focus(function(e) {              

            CurrentTextBoxID = this.id;
            var top = ($(window).height() - $('#keyboard').height()) - 25;        
            var left = ($(window).width() - $('#keyboard').width()) / 2;

            //alert(CurrentTextBoxID + " focus In");        
            $('#keyboard').css(
                {               
                    "left": left+"px",
                    "top": top+"px"
                }
            ).toggle();

            $('#keyboard').show();
        });


        //$(":text").focusout(function() {
        $(":text").focusout(function() {                

            alert($(this).attr('class'));

            if($(this).attr('class') != "testClass"){
                $('#keyboard').hide();
            }


        });

        // function thats called when any of the keys on the keyboard are pressed
        $("#keyboard input").bind("click", function(e) {                    
            $('#'+CurrentTextBoxID).replaceSelection($(this).val(), true);      
        }); 
    });
</script>

<style type="text/css">
#keyboard {
    position: fixed;
    background: #eee;
    display: none;
    border: 1px solid #ccc;
    border-radius:7px;
    width: 700px;
    height: 240px;
    padding: 5px;
    cursor: move;
    box-shadow: -5px -5px 5px 5px #888;
    -moz-border-radius: -5px -5px 5px 5px #888;
    -webkit-border-radius: -5px -5px 5px 5px #888;
}
.testClass{
    width: 100px;
}
</style>
</head>
<body>
<form id="frmOnScreenKeyboard" name="frmOnScreenKeyboard">  

<table border="0" cellpadding="0" cellspacing="0" >
<tr>
    <td>
        <input type="text" name="txtTest1" id="txtTest1"/>
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="txtTest2" id="txtTest2"/>      
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="txtTest3" id="txtTest3"/>      
    </td>
</tr>

<tr>
    <td>
        <input type="button" name="butButton1" id="butButton1"/>
    </td>
</tr>

<tr>
    <td>
        <input type="password" name="txtpassword1" id="txtpassword1"/>
    </td>
</tr>
<table>


<table height="900px">
</table>

<div id="keyboard">
    <table border=1 cellpadding=0 cellspacing=0>
        <tr>
            <td>
                <div id="row2_shift">
                    <table border=1 cellpadding=0 cellspacing=0>
                    <tr>                                
                        <td><input name="a" type="button" value="A" class="testClass" /></td>
                        <td><input name="s" type="button" value="S" class="testClass" /></td>
                        <td><input name="d" type="button" value="D" class="testClass" /></td>                                                           
                    </tr>
                    </table>
                </div>
            </td>
        </tr>               
    </table>        
</div>

</form>
</body>
</html>

当我使用此代码时,

alert($(this).attr('class'));

我仍然收到未定义的消息。 请再次指导我,谢谢大家。

最佳答案

试试这个 -

$(this).attr('class');

编辑 当元素没有类时会发生这种情况。您可以检查元素是否具有类 -

alert($(this).hasClass('classname'));

编辑2

我将您的代码添加到 JSFiddle 并在第一个文本框中聚焦,您可以看到带有类名称的警报。我观察到的最重要的事情是,您的文本输入都没有类属性。

您可以测试:http://jsfiddle.net/njJEg/4/

关于javascript - 点击按钮,显示css类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608356/

相关文章:

javascript - 向 Assets 添加参数?

javascript - react 使用手势和 react Spring 性能缓慢

javascript - 如何通过 PHP 脚本 jQuery XML 文件并正确解析它?

javascript - 单击更改链接图标

javascript - 如何更改标签的文本?

html - 使用 CSS 缩放整个 HTML 元素

javascript - 当没有互联网连接时,错误回调不会触发

javascript - 单个 Javascript 对象属性的多个值

javascript - 如何创建这个下拉框

javascript - 打开和关闭功能