首頁  >  文章  >  web前端  >  js和jquery透過this取得html標籤中的屬性值

js和jquery透過this取得html標籤中的屬性值

一个新手
一个新手原創
2017-09-30 09:35:591885瀏覽


<html>
    <head>
        <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
            /** JQUERY 通过当前标签属性名,获取属性的值   */
            function attrsByJquery(obj){                
            var v1 = $(obj).attr("dbname");                
            var v2 = $(obj).attr("name");
                alert(v1);
                alert(v2);
            }            
            /** JAVASCRIPT 通过当前标签属性名,获取属性的值 */
            function attrsByJs(obj){                
            var v1 = obj.getAttribute(&#39;name&#39;);                
            var v2 = obj.getAttribute(&#39;dbname&#39;);
                alert(v1);
                alert(v2);
            }            
            function attrsButtons(obj){                
            var v = obj.getAttribute(&#39;name&#39;);                
            var vv = $(obj).attr("myname");
                alert(v);
                alert(vv);
            }            
        </script>
    </head>
    <body>
    <p> <p style="width: 100px;
    height: 30px;
    float:left;
    margin-left:30px;
    " name="this is name feild" dbname="oracle1" onclick="attrsByJs(this)">JavaScript</p> <p style="width: 100px;
    height: 30px;
    float:left;
    margin-left:30px;
    " name="this is name feild" dbname="oracle2" onclick="attrsByJquery(this)">Juqery</p> 
    <input type="button" name="abcdeg" myname="this is my name" onclick="attrsButtons(this)" value="buttons"> 
    </p> 
    </body>
    </html>

以上是js和jquery透過this取得html標籤中的屬性值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn