我目前正在使用JavaScript在我的.html文件(實際上只是在#wrapper div)中查找所有數字的網站(個人)。我希望他們都被着色後。我的代碼如下:查找文檔中的所有數字並設置它們的樣式
window.onload=function(){
var str = document.getElementById("wrapper").innerHTML;
var patt =/\d+/g;
var res = str.match(patt);
res = str.replace(patt, "<span class='em'>" + res + "</span>");
document.getElementById("wrapper").innerHTML = res;
window.alert(res)
};
這段代碼的結果是,每個數字被由包含在一排,這顯然不是我想要的東西中所標示的地區的每一個數字的字符串替換。 我甚至用for-loop試過了,但那也沒用。
我感謝您的幫助!
已經使用javascript一年了,不知道'$&',謝謝你sir – aelor 2014-09-26 08:05:12
正是我在找的東西。非常感謝! – 2014-09-26 08:08:10