1. 程式人生 > >Js與標籤屬性 關於在JS中設定標籤屬性 js和jquery通過this獲取html標籤中的屬性值

Js與標籤屬性 關於在JS中設定標籤屬性 js和jquery通過this獲取html標籤中的屬性值

關於在JS中設定標籤屬性

2017-10-09 23:04 by 清風221, 12790 閱讀, 0 評論, 收藏編輯

Attribute

該屬性主要是用來在標籤行內樣式,新增、刪除、獲取屬性。且適用於自定義屬性。

setAttribute("屬性名",屬性值“”);這個是用來設定標籤屬性的;

removeAttribute("屬性名");這是用來刪除標籤屬性的。

getAttribute("屬性名");獲取標籤該屬性的值;

 

obj.style.css樣式

該屬性主要是用來在標籤行內樣式,新增、刪除、獲取屬性。不適用自定義屬性。

例如設定li標籤的寬高,背景色;

li.style.width = "200px";

li.style.height = "200px";

li.style.backgroundColor = "red";

注意在js中,css的屬性名用連線符的地方,改為將後面的第一個字母大寫,如:background-color ==> backgroundColor ; 

js和jquery通過this獲取html標籤中的屬性值

<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('name');
                var v2 = obj.getAttribute('dbname');
                alert(v1);
                alert(v2);
            }
            
            function attrsButtons(obj){
                var v = obj.getAttribute('name');
                var vv = $(obj).attr("myname");
                alert(v);
                alert(vv);
            }
            
        </script>
    </head>
    <body>
        <div>
            <div style="width: 100px;height: 30px;float:left; margin-left:30px;" name="this is name feild" dbname="oracle1" onclick="attrsByJs(this)">JavaScript</div>
            <div style="width: 100px;height: 30px;float:left; margin-left:30px;" name="this is name feild" dbname="oracle2" onclick="attrsByJquery(this)">Juqery</div>
            <input type="button" name="abcdeg" myname="this is my name" onclick="attrsButtons(this)" value="buttons">
        </div>
    </body>
</html>

 

window.getComputedStyle(obj)["屬性名"]

用來獲取標籤的屬性,對內聯式有效。

Attribute

該屬性主要是用來在標籤行內樣式,新增、刪除、獲取屬性。且適用於自定義屬性。

setAttribute("屬性名",屬性值“”);這個是用來設定標籤屬性的;

removeAttribute("屬性名");這是用來刪除標籤屬性的。

getAttribute("屬性名");獲取標籤該屬性的值;

 

obj.style.css樣式

該屬性主要是用來在標籤行內樣式,新增、刪除、獲取屬性。不適用自定義屬性。

例如設定li標籤的寬高,背景色;

li.style.width = "200px";

li.style.height = "200px";

li.style.backgroundColor = "red";

注意在js中,css的屬性名用連線符的地方,改為將後面的第一個字母大寫,如:background-color ==> backgroundColor ; 

js和jquery通過this獲取html標籤中的屬性值

<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('name');
                var v2 = obj.getAttribute('dbname');
                alert(v1);
                alert(v2);
            }
            
            function attrsButtons(obj){
                var v = obj.getAttribute('name');
                var vv = $(obj).attr("myname");
                alert(v);
                alert(vv);
            }
            
        </script>
    </head>
    <body>
        <div>
            <div style="width: 100px;height: 30px;float:left; margin-left:30px;" name="this is name feild" dbname="oracle1" onclick="attrsByJs(this)">JavaScript</div>
            <div style="width: 100px;height: 30px;float:left; margin-left:30px;" name="this is name feild" dbname="oracle2" onclick="attrsByJquery(this)">Juqery</div>
            <input type="button" name="abcdeg" myname="this is my name" onclick="attrsButtons(this)" value="buttons">
        </div>
    </body>
</html>

 

window.getComputedStyle(obj)["屬性名"]

用來獲取標籤的屬性,對內聯式有效。

<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('name');
                var v2 = obj.getAttribute('dbname');
                alert(v1);
                alert(v2);
            }
            
            function attrsButtons(obj){
                var v = obj.getAttribute('name');
                var vv = $(obj).attr("myname");
                alert(v);
                alert(vv);
            }
            
        </script>
    </head>
    <body>
        <div>
            <div style="width: 100px;height: 30px;float:left; margin-left:30px;" name="this is name feild" dbname="oracle1" onclick="attrsByJs(this)">JavaScript</div>
            <div style="width: 100px;height: 30px;float:left; margin-left:30px;" name="this is name feild" dbname="oracle2" onclick="attrsByJquery(this)">Juqery</div>
            <input type="button" name="abcdeg" myname="this is my name" onclick="attrsButtons(this)" value="buttons">
        </div>
    </body>
</html>