簡體   English   中英

如何向HTML標記添加屬性

[英]How do I add attributes to an HTML tag

我一直在嘗試使用JQuery找到一種向HTML標記添加屬性的方法,所以假設我有以下HTML代碼:

<input type="text" name="j_username" id="j_username" autocorrect="off" autocapitalize="off" style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: 0px 0px; background-repeat: no-repeat;">

和:

<input type="password" name="j_password">

我希望能夠將這兩者替換成以下內容:

<input type="text" name="j_username" id="j_username" value="" autofocus required style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: -298px 0px; background-repeat: no-repeat;" placeholder="LDAP Username">

和:

<input type="password" name="j_password" autofocus required style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: -298px 0px; background-repeat: no-repeat;" placeholder="LDAP Password">

我在網上找到的就是這樣的:

http://api.jquery.com/append/

要么:

使用jQuery更改HTML標記?

但是這兩個與我正在嘗試做的不同,因為第一個是在<tag></tag>之間添加新東西,第二個是從<tag></tag>替換為<anotherTag></anotherTag>

我是JQuery的新手,所以請在這個問題上跟我一起..:D

編輯:這是我試圖修改以添加上述的JS文件(供參考)

function checkToggle(e) {
    e.prop("checked",!0)
}

function writeCookie(e,t,n) {
    var o,r;n?(o=new Date,o.setTime(o.getTime()+1e3*60*60*24*n),r="; expires="+o.toGMTString()):r="",
    document.cookie=e+"="+t+r+"; path=/"
}

function readCookie(e) {
    var t,n,o,r=e+"=";
    for(o=document.cookie.split(";"),t=0; o.length>t; t++) {
        for(n=o[t]; " "==n.charAt(0);)
            n=n.substring(1,n.length);
        if(0==n.indexOf(r))
            return n.substring(r.length,n.length)
    }
    return""
}
jQuery (
    function(e) {
        function t(t) {
            return e("<div />",{"class":t})
        }
        if ("/login"==window.location.pathname) {
            var n="Welcome to Jenkins";
            // var user='autofocus required="" style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: -298px 0px; background-repeat: no-repeat;" placeholder="LDAP Username"';
            // var pass='autofocus required="" style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: -298px 0px; background-repeat: no-repeat;" placeholder="LDAP Password"';
            e("#main-panel"),e("#side-panel");
            var o=e('input[name="j_username"]').closest("td").prev("td").addClass("input-label"),r=e('input[name="j_password"]').closest("td").prev("td").addClass("input-label");
        //  e('input').replaceTag($('<input autofocus="" required="" style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: -298px 0px; background-repeat: no-repeat;">').addClass('wasDiv'), true);
            e("a[href=signup]").closest("div").addClass("signup-container"),e("#yui-gen1-button").addClass("login-button");
            var i=e(".submit-button"),a=e("#main-panel div:first").addClass("login-container"),c=e('form[name="login"]').length;
            c&&(o.text(""),r.text(""),a.removeAttr("style"),a.wrap(t("login-outer")),a.wrap(t("login-wrapper").prepend(" <h2> "+n+" </h2> ")),i.wrap(t("button-wrapper")),e(".login-container div:last").removeAttr("style"))
        }
    }
),

jQuery (
    function(e) {
        var t='<div class="toggle"></div><input id="cmn-toggle-7" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"><label for="cmn-toggle-7" data-on="Hide" data-off="Show"></label>';
        e("#description-link").text("edit description"),e("#description").before(t),"checked"!=readCookie("toggle")?e("#description").hide():(checkToggle(e(".cmn-toggle")),e("#description").show()),e(".cmn-toggle").change(function() {
            e("#description").slideToggle("fast"),e(".cmn-toggle").is(":checked")?writeCookie("toggle","checked",30):writeCookie("toggle","unchecked",30)
        })
    }
),

jQuery (
    function() {
        function e(e) {
            var t=jQuery(e.target),n=t.attr("name"),r=t.attr("value"),i=t.is(":checked");
            "on"===r&&(i=!1),document.cookie=n+"="+o(r+i)
        }

        function t(e) {
            var t=jQuery(e.target),r=t.attr("name"),i=t.attr("value"),a=t.is(":checked");
            return n(r)===o(i+a)?!0:!1
        }

        function n(e) {
            var t,n,o,r=e+"=";
            for (o=document.cookie.split(";"),t=0; o.length>t; t++) {
                for (n=o[t]; " "==n.charAt(0);)
                    n=n.substring(1,n.length);
                if(0==n.indexOf(r))
                    return n.substring(r.length,n.length)
            }
            return""
        }

        function o(e) {
            for(var t=e.length,n=[],o=0; t>o; o++) {
                var r=e[o].charCodeAt().toString(2);
                n.push(Array(8-r.length+1).join("0")+r)
            }
            return n.join(" ")
        }

        var r="#dfd",i="#fff";
        jQuery ('.setting-input, input[type="checkbox"]').one("click",function(t) {
            e(t)
        }),
        jQuery(".setting-input").on("input propertychange paste",function(e) {
            t(e)?jQuery(e.target).css("background-color",i):jQuery(e.target).css("background-color",r)
        }),
        jQuery('input[type="checkbox"]').change(function(e) {
            t(e)?jQuery(e.target.parentNode).css("background-color",i):jQuery(e.target.parentNode).css("background-color",r)
        })
    }
);

正如你所看到的,我試圖做幾件事,但最終失敗了......

EDIT2:感謝Milney的回答,我最終實現了我所需要的! 這就是現在的樣子:

jQuery (
        function(e) {
                function t(t) {
                        return e("<div />",{"class":t})
                }
                if ("/login"==window.location.pathname) {
                        var n="Welcome to Jenkins";
                        e("#main-panel"),e("#side-panel");
                        var o=e('input[name="j_username"]').attr({placeholder:"LDAP Username",autofocus:"",required:""}).closest("td").prev("td").addClass("input-label"),r=e('input[name="j_password"]').attr({id:"j_password",placeholder:"Password",autofocus:"",required:""}).closest("td").prev("td").addClass("input-label");
                        e("a[href=signup]").closest("div").addClass("signup-container"),e("#yui-gen1-button").addClass("login-button");
                        var i=e(".submit-button"),a=e("#main-panel div:first").addClass("login-container"),c=e('form[name="login"]').length;                        c&&(o.text(""),r.text(""),a.removeAttr("style"),a.wrap(t("login-outer")),a.wrap(t("login-wrapper").prepend(" <h2> "+n+" </h2> ")),i.wrap(t("button-wrapper")),e(".login-container div:last").removeAttr("style"));
                }
        }
),

.attr方法既可用於讀取和操作屬性。 這應該很簡單。 嘗試閱讀該方法的手冊頁: http//api.jquery.com/attr/

如評論中所述,考慮使用CSS類,因為將樣式屬性添加到許多單個元素可能會很快變得混亂。

您可以運行循環並使用第一個元素交叉檢查所有屬性,如果它不存在,您可以將其添加到第二個元素

//your first element
$('#j_username').each(function() {
  $.each(this.attributes, function() {
    if(this.specified) {
      var name = this.name;
      var value = this.value;
      var flag = 0;

      //your second element
      $('#j_password').each(function() {
        $.each(this.attributes, function() {
          if(this.specified) {
            if(this.name == name) {
                flag = 1;
            }
          }
        });
        if (flag == 0) {
            $('#j_password').attr(name, value);
        }
      });
    }
  });
});

這是您的HTML元素

<input type="text" name="j_username" id="j_username" autocorrect="off" autocapitalize="off" style="background-image: linear-gradient(rgba(255, 255, 255, 0) 96%, rgb(41, 140, 196) 4%); background-position: 0px 0px; background-repeat: no-repeat;">
<input type="password" name="j_password" id='j_password'>

但是您需要一個類或密碼才能訪問第二個元素

這是一個演示

您可以使用.attr()函數添加屬性

$("#j_username").attr("placeholder","LDAP Username");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM