當文檔加載完成時,我確實需要知道輸入的屬性。例如:如何知道文檔加載javascript時輸入的屬性
<input type="text" data-type="string" />
<input type="text" data-type="string" />
<input type="text" data-type="date" />
而且與警報的JavaScript應顯示
you have 2 string data-type and 1 date data-type
當文檔加載完成時,我確實需要知道輸入的屬性。例如:如何知道文檔加載javascript時輸入的屬性
<input type="text" data-type="string" />
<input type="text" data-type="string" />
<input type="text" data-type="date" />
而且與警報的JavaScript應顯示
you have 2 string data-type and 1 date data-type
在被提名爲給你太多代碼的風險......因爲你的問題還沒有被標記jQuery
一純Javascript實現可能如下:
(function(){
var loader = window.onload;
window.onload = function(){
if(typeof loader == 'function') loader();
var els = document.getElementsByTagName('input'), data = [];
for(var i=0;i<els.length;i++){
var type = els[i].getAttribute('data-type') || 'unknown';
if(typeof data[type] == 'undefined') data[type] = 1;
else data[type]++;
}
var out = '';
for(var key in data){
if(out != '') out += 'and '; else out += 'You have ';
out += data[key] + ' ' + key + ' data-type ';
}
alert(out);
}
})();
有些比較繞口,也許令人困惑 - 但你應該張貼任何進一步的問題,請包括代碼示例你已經嘗試和狀態正是你模糊。
使用jQuery。
$.each($("input") , function(_index, _item){
alert($(_item).attr("data-type"));
});
你剛纔要算並顯示您的郵件...
對於複雜的數據,你可以使用'$(_ item).data('type')'[differences](http://stackoverflow.com/a/7262427/2033671) – 2013-03-15 20:35:32
是的,這是行之有效的,謝謝你那個建議,不好記住 – Misters 2013-03-17 11:45:24