我想通過<a>
標籤創建10個正文鏈接。在演示中,您可以看到只有3個鏈接的代碼太長,我相信有一種方法可以改進此代碼。如何使用Javascript或jQuery使此代碼更有效?
那麼,我怎麼知道哪個鏈接被選中,我該怎麼處理這些信息呢?
function fillYellow() {
var paintedDiv = document.getElementsByClassName('painted')[0];
paintedDiv.style.background = "yellow";
setTimeout(function() {
if (paintedDiv.style.background == "yellow") {
paintedDiv.style.background = "#e5e5e5";
}
}, 3000);
}
function fillYellow1() {
var paintedDiv = document.getElementsByClassName('painted')[1];
paintedDiv.style.background = "yellow";
setTimeout(function() {
if (paintedDiv.style.background == "yellow") {
paintedDiv.style.background = "#e5e5e5";
}
}, 3000);
}
function fillYellow2() {
var paintedDiv = document.getElementsByClassName('painted')[2];
paintedDiv.style.background = "yellow";
setTimeout(function() {
if (paintedDiv.style.background == "yellow") {
paintedDiv.style.background = "#e5e5e5";
}
}, 3000);
}
DEMO:http://jsbin.com/panarupo/1/edit
您想更高效嗎?使用合理的縮進! – Bergi
懶人們有一個工具:http://jsbeautifier.org/。 – leaf
我自己做的... – leaf