我來不及一如既往,但我會用HTML 5的數據集屬性:
<!DOCTYPE html>
<html>
<head>
<script>
function doSomething(classname,func,datakey)
{
var hrefs = document.getElementsByClassName(classname), // get all elements with the class
i = 0; // initialize the loop
for(;i < hrefs.length; i++) // iterate through all elements
{
func(hrefs[i].dataset[datakey]); // execute the passed function with the content of the dataset as first argument
}
}
function alertval(value) // example function to do something with it
{
alert(value);
}
window.onload = function()
{
doSomething("Test",alertval,"something"); // just test it
}
</script>
</head>
<body>
<a href="#" class="Test" data-something="FooBar_1">Test1</a>
<a href="#" class="Test" data-something="FooBar_2">Test2</a>
<a href="#" class="Test" data-something="FooBar_3">Test3</a>
</body>
</html>
使用document.getElementsByClassName – Choco 2014-10-31 10:15:07
是否要在此獲取數據屬性? – Choco 2014-10-31 10:19:04
Ty Choco,但是我怎樣才能得到屬性「data」對應用戶點擊的元素的值? (我希望它使得sence,我的英文不是很好) – 2014-10-31 10:19:49