簡體   English   中英

使用JavaScript獲取類中的屬性值

[英]getting an attribute-value in a class using javascript

我不知道是否在這里就問過這個問題,我只是不知道正確的詞。

我有這個輸入標簽:

 <input type = "text" class="inputbox holdout-7"></input>

如何使用javascript從類中獲取7的保持值?

這是因為我想添加自定義屬性,但是在呈現頁面時,不會顯示我的自定義屬性。 有些人建議我改為將它們放在課堂上。

例如:

<input type = "text" class = "inputbox" holdout="7"></input>

呈現頁面時,不包含保持,因此無法獲取該值。

var inputBox = document.querySelector(".inputbox"),
    classname = inputBox.className,
    regEx = /holdout-(\d+)/,
    holdoutValue = classname.match(regEx)[1];

它會回報你7

要將其設置為輸入框中的屬性:

inputBox.setAttribute("data-holdout",holdoutValue);

建議使用data-holdout而不是holdout

暫無
暫無

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

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